这段代码json_encode行没有回显
include "connect.php";
$sql="select * from customers";
$arr = array();
$result=mysqli_query($conn,$sql);
while ($row = mysqli_fetch_assoc($result))
{
$firstname = $row['firstname'];
$lastname = $row['lastname'];
$arr[] = array('lastname' => $lastname, 'lastname' => $lastname);
}
echo json_encode($arr);// results in nothing.
我做错了什么?请帮忙
答案 0 :(得分:1)
即使您在空数组上执行echo json_encode($arr);
,也应该看到结果[]
。所以问题可能在mysqli_代码中的某个地方。
请检查通过任何mysql数据库扩展所做的所有调用的状态。通过这种方式,当您出现某种错误时会通知您。
include "connect.php";
// this should probably go in the 'connect.php'
// this is just an example no a difinitive piece of code
function db_err(&$conn, &$sql, $lineNo)
{
if ( $conn_errno($conn) ) {
echo sprinf( 'Query %s failed on line %d Code:%d Message:%s',
$sql,
$lineNo,
mylsqi_errno($conn),
mylsqi_error($conn));
exit;
}
}
$sql="select * from customers";
$result = mysqli_query($conn,$sql);
db_err($conn,$sql, __LINE__);
$arr = array();
while ($row = mysqli_fetch_assoc($result)) {
$firstname = $row['firstname'];
$lastname = $row['lastname'];
$arr[] = array('lastname' => $firstname, 'lastname' => $lastname);
}
echo json_encode($arr);