使用PHP将数据从MYSQL转换为JSON
我有以下非常简单的测试PHP代码,用于提取数据并将其转换为JSON格式的文本 我得到以下错误 致命错误:第33行的/var/www/test.php中允许的内存大小33554432字节已用尽(尝试分配1979603字节) 其中第33行是使用PHP将数据从MYSQL转换为JSON,php,mysql,json,Php,Mysql,Json,我有以下非常简单的测试PHP代码,用于提取数据并将其转换为JSON格式的文本 我得到以下错误 致命错误:第33行的/var/www/test.php中允许的内存大小33554432字节已用尽(尝试分配1979603字节) 其中第33行是json\u encode()行 有没有办法让这更有效?PHP.ini的max已设置为32M,因此比8M标准大 <?php require('../../admin/db_login.php'); $db=mysql_connect($h
json\u encode()
行
有没有办法让这更有效?PHP.ini的max已设置为32M,因此比8M标准大
<?php
require('../../admin/db_login.php');
$db=mysql_connect($host, $username, $password) or die('Could not connect');
mysql_select_db($db_name, $db) or die('');
$result = mysql_query("SELECT * from listinfo") or die('Could not query');
$json = array();
if(mysql_num_rows($result)){
$row=mysql_fetch_assoc($result);
while($row=mysql_fetch_row($result)){
// cast results to specific data types
$test_data[]=$row;
}
$json['testData']=$test_data;
}
mysql_close($db);
echo json_encode($json);
?>
作为第一个解决方案,将其设置为大约256M甚至512M
MySQL返回给您的数据集可能很大。因此,即使您的PHP非常节省内存,您仍然会得到OoM错误。因此,作为更可行的长期解决方案,使用LIMIT
语句(SELECT*FROM$table,其中1=1 LIMIT 0,30
(从索引0开始,获取30项)
编辑:噢,哇,我甚至没有看到第一个解决方案中的问题……好吧,限制你的查询可能还是个好主意:-)停止复制你的数据数组
$json = array();
if(mysql_num_rows($result)){
$row=mysql_fetch_assoc($result);
while($row=mysql_fetch_row($result)){
// cast results to specific data types
$json['testData'][]=$row;
}
}
这将有助于减少内存使用您可能正在编码一个非常大的数据集。您可以编码每一行,一次一行,而不是在一个大的操作中编码它
<?php
require('../../admin/db_login.php');
$db=mysql_connect($host, $username, $password) or die('Could not connect');
mysql_select_db($db_name, $db) or die('');
$result = mysql_query("SELECT * from listinfo") or die('Could not query');
if(mysql_num_rows($result)){
echo '{"testData":[';
$first = true;
$row=mysql_fetch_assoc($result);
while($row=mysql_fetch_row($result)){
// cast results to specific data types
if($first) {
$first = false;
} else {
echo ',';
}
echo json_encode($row);
}
echo ']}';
} else {
echo '[]';
}
mysql_close($db);
使用这个:
$result = mysql_query("SELECT * FROM listinfo");
$json = array();
$total_records = mysql_num_rows($result);
if($total_records > 0){
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)){
$json[] = $row;
}
}
echo json_encode($json);
这是我的第一个完美工作的json
<?php
// connect to mysql server
mysql_connect($host, $username, $password) or die('Could not connect');
// select the db name
mysql_select_db($dbname);
// enter your sql query
$sql = "Select * from Order_Details";
// Creates temp array variable
$temp = array();
// Gets table details
$result = mysql_query($sql);
// Adds each records/row to $temp
while($row=mysql_fetch_row($result)) {
$temp[] = $row;
}
// Formats json from temp and shows/print on page
echo json_encode($temp);
?>
是的,喊得好!根本没意识到我在复制它!这已经用了16个字节,设置为64M,现在它适合:-)@Lee:非常规,但这样你就不必将每一行存储到一个数组中以供以后编码。是否有办法让它将行标题放在每一行上?@Lee:在每一行上都有“testData”
?是的,但你为什么要这样?您的原始代码不是这样运行的。不,使用列标题:columndata@Lee:当然!将while循环中的mysql\u-fetch\u行($result)
替换为mysql\u-fetch\u数组($result,mysql\u-ASSOC)
。为什么要使用if($total\u-records>=1){
和$total\u-records=mysql\u-num-rows($result)