我试图获取最后 100 行,但结果只显示了一行
这是我的 PHP 代码:
<?php
require_once("variables.php");
$db = new ezSQL_mysql($GLOBALS['database_username'],$GLOBALS['database_password'],$GLOBALS['database_database'],$GLOBALS['database_server']);
header('Content-Type: application/json');
echo "{\"office\":\"0\",\"dept\":{\"desk\":[";
$symbols = addslashes($_GET['symbols']);
$symbolsArr = explode(",", $symbols);
foreach($symbolsArr as $s) {
$last = $db->get_row("select * from data where name='$s' order by id desc limit 0,100");
$jsonArray = array('name' => $s,
'phone' => $last->phone,
'active' => $last->active);
echo json_encode($jsonArray);}
echo "]}}";
?>
更新> 根据以下建议,我已将 get_row 更改为 get_results,但代码现在已损坏,并且未显示任何错误。
最佳答案
阅读有关您正在使用的数据库类 (ezSQL) 的文档:
----------------------------------------------------
Example 2
----------------------------------------------------
// Get one row from the database..
$user = $db->get_row("SELECT name,email FROM users WHERE id = 2");
echo $user->name;
echo $user->email;
您发出的语句专门用于获取一行。
您要使用的是 get_results()
方法,而不是 get_row()
方法。
我重新阅读了您的问题,发现您执行的是 limit 0,100,它返回以 0(第一个)开头的 100 行。我以为你试图循环这些行,但我可以看到你实际上只是想得到最后一行......我的坏,derp ..好吧 - 你在你的查询中有点颠倒了 - 应该是 100,1 因为你想要第 100 行,只有 1 行。
$last = $db->get_row("select * from data where name='{$s}' order by id desc limit 100,1");
$jsonArray = array('name' => $s,
'phone' => $last->phone,
'active' => $last->active);
关于php - 从 mysql 中获取最后 100 行不工作 | JSON 格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25817103/