php Mysql 获取所有行并作为 json 回显
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15511367/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Mysql fetch all rows and echo as json
提问by Coop
I've got a database with 5 columns and multiple rows. I want to fetch the first 3 rows and echo them as an array. So far I can only get the first row (I'm new to PHP and mysql). Here's my PHP so far:
我有一个包含 5 列和多行的数据库。我想获取前 3 行并将它们作为数组回显。到目前为止,我只能获得第一行(我是 PHP 和 mysql 的新手)。到目前为止,这是我的 PHP:
//==== FETCH DATA
$result = mysql_query("SELECT * FROM $tableName");
$array = mysql_fetch_row($result);
//==== ECHO AS JSON
echo json_encode($array);
Help would be much appreciated.
帮助将不胜感激。
回答by Schleis
You need to loop through the results. mysql_fetch_rowgets them one at a time.
您需要遍历结果。 mysql_fetch_row一次得到一个。
http://php.net/manual/en/function.mysql-fetch-row.php
http://php.net/manual/en/function.mysql-fetch-row.php
The code would end up like:
代码最终会像:
$jsonData = array();
while ($array = mysql_fetch_row($result)) {
$jsonData[] = $array;
}
echo json_encode($jsonData);
//json_encode()
PLEASE NOTEThe mysql extension is deprecated in PHP 5.5, as stated in the comments you should use mysqli or PDO. You would just substitute mysqli_fetch_rowin the code above.
http://www.php.net/manual/en/mysqli-result.fetch-row.php
请注意mysql 扩展在 PHP 5.5 中已被弃用,如您应该使用 mysqli 或 PDO 的评论中所述。您只需替换mysqli_fetch_row上面的代码即可。
http://www.php.net/manual/en/mysqli-result.fetch-row.php
回答by stefreak
According to the PHP Documentationmysql_fetch_row(besides that it's deprecated and you should use mysqli or PDO)
根据PHP 文档mysql_fetch_row(除此之外它已被弃用,您应该使用 mysqli 或 PDO)
Returns a numerical array that corresponds to the fetched row and moves the internal data pointer ahead.
返回一个对应于所获取行的数值数组,并将内部数据指针向前移动。
so you need for example a while loop to fetch all rows:
所以你需要例如一个while循环来获取所有行:
$rows = array();
while ($row = mysql_fetch_row($result)) {
$rows[] = $row;
}
echo json_encode($rows);
I leave it to you how to only fetch 3 rows :)
我留给你如何只获取 3 行:)
回答by Jhollman
I do like this while quering an ODBC database connection with PHP 5.5.7, the results will be in JSON format:
在使用 PHP 5.5.7 查询 ODBC 数据库连接时,我喜欢这样做,结果将采用 JSON 格式:
$conn = odbc_connect($odbc_name, 'user', 'pass');
$result = odbc_exec($conn, $sql_query);
Fetching results allowing edit on fields:
获取允许编辑字段的结果:
while( $row = odbc_fetch_array($result) ) {
$json['field_1'] = $row['field_1'];
$json['field_2'] = $row['field_2'];
$json['field_3'] = $row['field_1'] + $row['field_2'];
array_push($response, $json);
}
Or if i do not want to change anything i could simplify like this:
或者如果我不想改变任何东西,我可以像这样简化:
while ($array = odbc_fetch_array($result)) { $response[] = $array; }
What if i want to return the results in JSON format?, easy:
如果我想以 JSON 格式返回结果怎么办?,简单:
echo json_encode($response, true);
You can change odbc_fetch_array for mysqli_fetch_array to query a MySql db.
您可以将 odbc_fetch_array 更改为 mysqli_fetch_array 以查询 MySql 数据库。
回答by dcbarans
You need to put this in some kind of a loop, mysql_fetch_row returns results one at a time.
您需要将其放入某种循环中,mysql_fetch_row 一次返回一个结果。
See example: http://www.php.net/manual/en/mysqli-result.fetch-row.php#example-1794
参见示例:http: //www.php.net/manual/en/mysqli-result.fetch-row.php#example-1794
回答by Manomite
$result = mysql_query( "SELECT * FROM $tableName ORDER BY id LIMIT 3");
$result = mysql_query("SELECT * FROM $tableName ORDER BY id LIMIT 3");
$json = array(); while($array = mysql_fetch_row($result)){
$json = 数组(); while($array = mysql_fetch_row($result)){
$json[] = $array; }
$json[] = $array; }
echo json_encode($json);
回声 json_encode($json);

