Php Can't Populate Array From Selected Table To Json_encode And Receive It On Javascript
Solution 1:
Found the solution here on this post Everything on the DB must be set to UTF-8 or utf8mb4 and now it's finally working. Apreciated everyone's help :)
Solution 2:
Try something like this...
If you want only to get the values of column 'Tipo',
$sql=mysql_query("SELECT Tipo FROM tfc_db.Category ");
$id=array();
$i=0;
while($row = mysql_fetch_assoc($sql)) {
$id["data".$i] = $row['Tipo'];
$i++;
}
echo json_encode($id);
mysql_close($conn);
If you awnt to get all the values,
$sql=mysql_query("SELECT Tipo FROM tfc_db.Category ");
$id=array();
$i=0;
while($row = mysql_fetch_assoc($sql)) {
foreach($valueas$key=>$value){
$id["data".$i][$key] = $value;
}
$i++;
}
echo json_encode($id);
mysql_close($conn);
Solution 3:
You can use the following query:
$sql=mysql_query("SELECT Tipo FROM tfc_db.Category ");
$id=array();
while($row = mysql_fetch_assoc($sql)) {
$id[] = $row['Tipo'];
}
echo json_encode($id);
mysql_close($conn);
It will return the categories as an array of strings in JSON. e.g. ["entry 1","entry 2"]. If you decide to use this then you would need to adjust your Javascript code. Your javascript code contains some problems on this line:
$('#list').append("<li><h3>"+answer.tipo+"</h3></a></li>");
In the case of you using
$id[] = $row;
In PHP, then the column name would be named Tipo, not tipo (case sensitive). The second issue is that you are trying to access answer.tipo and not the element of the array answer[i].tipo.
So with your current php code you can make it working by changing the javascript. This line
$('#list').append("<li><h3>"+answer.tipo+"</h3></a></li>");
To
$('#list').append("<li><h3>"+answer[i].Tipo+"</h3></a></li>");
Edit: Since your apparently having issues here is the full code I used.
//query.php<?php
mysql_connect ('localhost','username','password');
mysql_select_db('db_test');
$sql=mysql_query("SELECT Tipo FROM db_test.Category ");
$id=array();
while($row = mysql_fetch_assoc($sql)) {
$id[] = $row;
}
echo json_encode($id);
mysql_close($conn);
?>
<html><head><title> JQuery db test </title><scriptsrc="http://code.jquery.com/jquery-2.1.1.min.js"></script></head><body><divid='list'class='list'></div><script>var debug;
$.get('http://www.example.com/test/query.php',{
},function(answer){
debug=answer;
//alert(answer.length+" "+answer); //trying to know whats happening with the data.for (var i = 0; i < answer.length; i++) { //my final objective is to fill a listview.
$('#list').append("<li> <h3>"+answer[i].Tipo+"</h3></a></li>");
}
// $('#list').listview('refresh');
},"json"
);
</script></html>
Solution 4:
[PHP]
$id = array();
while($row = mysql_fetch_assoc($sql))
$id[] = $row['Tipo'];
echo json_encode($id);
[JS]
$(document).ready(function(){
$('#submit').click(function(e){
$.ajax({
url: '/get_category.php',
type: 'GET',
dataType: 'json'
}).done(function(res){
$('#list').html('');
for(var i=0;i<res.length;++i)
$('#list').append('<li><h3>'+res[i]+'</h3></li>');
});
});
Post a Comment for "Php Can't Populate Array From Selected Table To Json_encode And Receive It On Javascript"