[英]How to return all result rows resulting from a SQL statement execution to the page where the AJAX request came from
如何在MySQL + PHP中获取所有结果行?
您知道,如果我使用mysql_fetch_assoc()
,它将仅返回一个结果行作为关联数组。 我正在使用AJAX来获取MySQL表中的数据。
$result=mysql_query("select * from questions where announcementid='$taskid'")or die(mysql_error());
如何将$ result的值返回到激发Ajax请求的页面的数组?
// 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/zh/function.json-encode.php
然后,您可以在客户端的javascript代码中以数组形式访问数据。
我认为您可以在PHP代码中添加while
函数,并执行rikh建议的操作 :
$results = array();
while ( $row = mysql_fetch_assoc( $result )) {
$results[] = $row;
}
// output them, encoded as a javascript object
echo json_encode( $results );
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.