简体   繁体   中英

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 );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM