我的db表中有3行但是当我用ajax询问我的数据时,我有6行。 这怎么可能?
在前3个ajax调用中我有完整的db信息。在接下来的3个ajax调用中我有正确的信息,我在php for loop中要求
$db = new PDO('mysql:host=localhost;dbname=projectmobilewebappstest;charset=utf8', 'eclipse', 'eclipse');
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$query = $db->prepare('SELECT * FROM poll');
$query->execute();
$result = $query -> fetchAll();
foreach( $result as $row ) {
$result[] = array('poll' => $row['poll']
,'Yes' => $row['Yes']);
}
echo json_encode($result);
var form = $.ajax({
url: "server.php/lijst",
type: "post",
dataType: "json",
encode: true
})
form.done(function (response) {
console.log(response[3].Yes)
$("#mydata").html("");
for(var i = 0; i < response.length;i++)
{
console.log(response[i])
$("#mydata").append(response[4].poll);
$("#mydata").append("</br>");
}
答案 0 :(得分:0)
我认为你在这里做了一些令人困惑的事情:
foreach( $result as $row ) {
$result[] = array('poll' => $row['poll']
,'Yes' => $row['Yes']);
}
答案 1 :(得分:0)
这里有一个例子可能会帮助你
$id_user=$_POST['id_user'];
$bdd = new PDO('mysql:host=localhost;dbname=ailyan', 'root', '');
$req = $bdd->prepare('select * from event where id_user = ?');
$req->execute(array($id_user));
//$_SESSION['id']
$response["events"] = array();
while( $donnees = $req->fetch()){
$endroits = array();
$endroits ['titre']=$donnees['titre'];
$endroits ['date']=$donnees['date'];
array_push($response["events"], $endroits);
}
print json_encode($response);
其余的:
$.ajax({
type:"POST",
url:"traitement/event/recuperer_livraison.php",
data:{id_user:id_user},
success:function(data){
var obj = jQuery.parseJSON(data);
jQuery.each(obj["events"], function(index, value) {
console.log(value.titre);
console.log(value.date);
})
}
});