我使用PHP文件从MySQL数据库输出JSON数据。当我只拉大约50行时,我的代码工作得很好,但是我的数据库包含超过12,000行。有没有人知道如何在没有我的服务器返回"死亡的白色屏幕的情况下提取更多数据"?大声笑。我目前的代码如下:
<?php
//Create Database connection
$db = mysql_connect("localhost","username","password");
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 limit 50", $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['customerphone'] = $row['customerphone'];
$row_array['customercity'] = $row['customercity'];
$row_array['customeraddress'] = $row['customeraddress'];
$row_array['lastupdate'] = $row['lastupdate'];
//push the values in the array
array_push($json_response,$row_array);
}
echo json_encode($json_response);
//Close the database connection
fclose($db);
?>
答案 0 :(得分:0)
如果前50行进展顺利,问题可能出在数据上...... 我认为是恶意单引号或其他什么
您可以使用限制来尝试不同的数据集。 例如,如果你想检查下一个50并跳过前50个限制
$result = mysql_query("select * from customer limit 50, 50", $db);
我认为你应该尝试1到50,50到100的集合,然后缩小到下一个10并找出哪个集合然后哪个行/记录给出问题然后找出解决问题的json编码问题区域。
答案 1 :(得分:0)
您可以根据GET / POST参数输出json数据,方法是在sql表中对结果进行分页。例如,如果你想要从第50到第100行,你可以做这样的事情:
<?php
//Create Database connection
$mysqli = new mysqli("localhost", "username", "password", "dbname");
if ($mysqli->connect_errno) {
die('Could not connect to db: ' . $mysqli->connect_error);
}
// Get the page we are interested in
$page = $_REQUEST['page']; // E.g. 1
//Replace * in the query with the column names.
$result = $mysqli->query("SELECT * FROM customer LIMIT ".(50*$page).",50");
//Create an array
$json_response = array();
while ($row = $result->fetch_assoc()) {
//push the values in the array
array_push($json_response,$row);
}
echo json_encode($json_response);
// free result set
$result->close();
//Close the database connection
$mysqli->close();
?>