我已使用以下代码从MySQL表生成JSON。当我只生成2个数组时它很有用,但出于某种原因,当我尝试生成3个或4个数组时,我得到了死亡的白色屏幕"。我认为它发生了,因为我需要提高我的PHP内存限制,但我已经完成了这个并且仍然遇到了同样的问题。见下文:
有效的代码是:
<?php
//Create Database connection
$db = mysql_connect("localhost","user","dbpassword");
if (!$db) {
die('Could not connect to db: ' . mysql_error());
}
//Select the Database
mysql_select_db("dbname",$db);
//Replace * in the query with the column names.
$result = mysql_query("select * from customer", $db);
//Create an array
$json_response = array();
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$row_array['customerfname'] = $row['customerfname'];
$row_array['customerlname'] = $row['customerlname'];
//push the values in the array
array_push($json_response,$row_array);
}
echo json_encode($json_response);
//Close the database connection
fclose($db);
?>
但是当我尝试调用更多值时,这就是我得到空白屏幕的地方:
<?php
//Create Database connection
$db = mysql_connect("localhost","user","dbpassword");
if (!$db) {
die('Could not connect to db: ' . mysql_error());
}
//Select the Database
mysql_select_db("dbname",$db);
//Replace * in the query with the column names.
$result = mysql_query("select * from customer", $db);
//Create an array
$json_response = array();
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$row_array['customerfname'] = $row['customerfname'];
$row_array['customerlname'] = $row['customerlname'];
$row_array['customeremail'] = $row['customeremail'];
$row_array['customertel'] = $row['customertel'];
//push the values in the array
array_push($json_response,$row_array);
}
var_dump($json_response);
echo json_encode($json_response);
?>
更新**
错误似乎出现在我的最后一行(echo json_encode($ json_response);
在最后一行之前添加 print_r($ row)时会生成以下输出。
Array ( [id] => 1 [customerfname] => First [customerlname] => Last [customeremail] => test@test.com [customerphone] => 000-000-0000
更新2
在 echo json_encode 行之前添加 var_dump($ json_response); 会导致空白屏幕。
答案 0 :(得分:0)
当您使用会导致某些麻烦的flcose($db)
时。删除该行。
flcose用于关闭资源,例如写入文本文件。
关闭连接使用mysql_close()
:
mysql_close($db);
并且你的循环可以减少到这个:
//Create an array
$json_response = array();
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
//push the values in the array
array_push($json_response,$row);
}
//Close the database connection
mysql_close($db);
echo json_encode($json_response);
我可能会在关闭连接后回应json,但这并不重要。
我发现white screen of death
非常有趣:D
答案 1 :(得分:0)
供将来参考:如果由于遇到类似问题而有其他人正在访问此问题,则由于查询返回了太多行并且json_encode()
没有,因此出现空白页错误似乎能够处理它。
尝试以下操作,并与我们分享您的结果,
<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);
//Create Database connection
$db = mysql_connect("localhost","user","dbpassword");
if (!$db)
die('Could not connect to db: ' . mysql_error());
mysql_select_db("dbname",$db);
mysql_query('SET CHARACTER SET utf8');
$result = mysql_query("select `customerfname`, `customerlname`, `customeremail`, `customertel` from customer", $db) or die("Error: ".mysql_error());
$json_response = array();
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$json_response[] = $row;
}
var_dump($json_response);
/* If the var_dump works, you can try and uncomment this section
if (function_exists('json_encode'))
{
echo json_encode($json_response);
echo "JSON Error: ".json_last_error();
}
else { echo "json_encode() is not supported"; }
*/
?>