How to return all result rows resulting from a SQL statement execution to the page where the AJAX request came from
How to fetch all result rows in MySQL+PHP?
You know, if I use mysql_fetch_assoc()
, it only returns one result row as an associative array. I am using AJAX to fetch the data in a MySQL table.
$result=mysql_query("select * from questions where announcementid='$taskid'")or die(mysql_error());
How to return the value of $result which is an array to the 开发者_运维问答page where an Ajax request was fired?
// fetch the results from mysql
$row = mysql_fetch_assoc( $result );
// output them, encoded as a javascript object
echo json_encode( $row );
http://php.net/manual/en/function.json-encode.php
You can then access the data as an array in your javascript code on the client side.
I think you could add a while
function to the PHP code, and do the same that rikh suggested:
$results = array();
while ( $row = mysql_fetch_assoc( $result )) {
$results[] = $row;
}
// output them, encoded as a javascript object
echo json_encode( $results );
精彩评论