2013-03-11 41 views
0
$res = mysql_query("SELECT * FROM `basket` WHERE `account_id` = '$_SESSION[ID]'"); 
while($row = mysql_fetch_assoc($res)) { 
     $search = $row['number']; 
     echo $search; 
} 

什麼我試圖做的是使用Ajax調用這個PHP文件中,然後將「遙相呼應」的數據我想分析和地方一個div裏面,這裏的Ajax調用返回的foreach響應

$(document).ready(function() { 
    var data = "func=basket";    
    $.ajax({ 
     url: "functions/ajax.php", 
     data: data, 
     type: "GET", 
     success: function(data, textStatus, jqXHR){ 
      $("#response").html(data); 
     }, 
     error: function (jqXHR, textStatus, errorThrown){ 
      console.log('Error ' + jqXHR); 
     } 
    }); 
}); 

但我遇到的問題是,直到每個while($row etc..處理它不解析數據,我想通過1到回波第1行中,我如何去這樣做

+1

可能重複的[jQuery的ajax,閱讀流增量?](http://stackoverflow.com/questions/7740646/jquery-ajax-read-the-stream-incrementally) – 2013-03-11 17:19:45

+0

有點像'yield'會是在這裏完美;) - 但你可以使用[flush](http://php.net/flush)。我會回答。 – 2013-03-11 17:20:27

+0

你在這裏使用'mysql_query'的任何原因? – tadman 2013-03-11 17:23:20

回答

1

如何採取了不同的方法......

while($row = mysql_fetch_assoc($res)) { 
    $search[] = $row['number']; 

}

echo json_encode($search); 

在你成功的回調函數,你只需通過使用jQuery的每一個()你的籃子要素的json_encoded陣列迭代

loop through json array jquery

http://api.jquery.com/jQuery.each/

1

功能你'在這裏尋找被稱爲flush,這將允許你沖洗wr你的web服務器的ite緩衝區(Apache,Nginx等)。

$pdo = new PDO('mysql:dbname=yourdb;host=127.0.0.1', 'root', ''); 
$sth = $pdo->prepare('SELECT * FROM `basket` WHERE `account_id`=?'); 
$sth->bindParam(1, $_SESSION['id'], PDO::PARAM_INT); 
$sth->execute(); 

while($row = $sth->fetch(PDO::FETCH_ASSOC)) 
{ 
    echo $row['number']; 
    flush(); 
} 

您還使用您應該考慮PDO或庫MySQLi更換這些廢棄的函數(mysql_*)(我在我的崗位做這個給你)。

注意:某些瀏覽器在顯示內容之前需要一定數量的數據。

+0

我已更新我的帖子以使用PDO。 – 2013-03-11 17:35:02

+0

非常感謝您的回覆,儘管PDO不是我的使用方式,我堅持使用mySQLi;),我只在mySQL中發佈了簡單查看 – 2013-03-11 17:40:34