I'm trying to use $.post
to retrieve multiple data from MySQL. I can't seem to get this working. How do I retrieve multiple data using jquery-ajax from MySQL?
php
$e = $_POST['stu'];
$sq ="SELECT physics, chemistry, agriculture FROM subjects WHERE student = :student";
$stmt = $getdb->prepare($sq);
$stmt->execute(array(':student'=>"123456"));
$rslt = $stmt->fetchAll();
$sd=array();
foreach($rslt as $val){
$sd[] = $val;
}
echo json_encode($sd);
jq:
$.post('my.php',
{
stu:"test"
},
function(data){
$.each(data,function(ab){
alert(ab.physics+" || "+ab.chemistry+" || "+item.agriculture);
});
});
EDIT
console.log(data);
Retrieve multiple data
means ? – Pratik C Joshi Jun 17 '15 at 17:51print_r($rslt)
after the fetch to see what is there. If it is an array (as it should be) you can skip the$sd
array forming and justecho json_encode($rslt)
– Jay Blanchard Jun 17 '15 at 18:00console.log(data)
. Your array has now changed and you'll need to figure out how to parse it. – Jay Blanchard Jun 17 '15 at 18:07$.each(data[0], function....
– Jay Blanchard Jun 17 '15 at 18:37