gpt4 book ai didi

php - MySQL 的 JSON 输出有效,但随后出现白屏?

转载 作者:行者123 更新时间:2023-11-30 22:58:08 25 4
gpt4 key购买 nike

我使用以下代码从 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); 会导致空白屏幕。

最佳答案

供日后引用:如果其他人因为有类似问题访问此问题,则出现空白页错误,因为查询返回了太多行和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"; }
*/
?>

关于php - MySQL 的 JSON 输出有效,但随后出现白屏?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25334983/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com