Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/291.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP脚本使用超过512MB时,只处理几个MB的数组_Php_Mysql_Sql Server_Memory_Memory Limit - Fatal编程技术网

PHP脚本使用超过512MB时,只处理几个MB的数组

PHP脚本使用超过512MB时,只处理几个MB的数组,php,mysql,sql-server,memory,memory-limit,Php,Mysql,Sql Server,Memory,Memory Limit,我用Apache在Ubuntu上运行PHP。我有一个简单的脚本,它只是执行一个查询,从数据库中提取一组纬度和经度对,将它们存储到一个数组中,然后echo输出JSON 该阵列大约有40000个元素。数组中的一个条目如下所示 {"lat":"41.951234015","lng":"-87.5317308"}. 如果相关的话,查询将通过使用链接服务器的MSSQL数据库发送到MySQL数据库 如果我执行ini\u集('memory\u limit','512M'),PHP将耗尽内存,但如果执行in

我用Apache在Ubuntu上运行PHP。我有一个简单的脚本,它只是执行一个查询,从数据库中提取一组纬度和经度对,将它们存储到一个数组中,然后echo输出JSON

该阵列大约有40000个元素。数组中的一个条目如下所示

{"lat":"41.951234015","lng":"-87.5317308"}. 
如果相关的话,查询将通过使用链接服务器的MSSQL数据库发送到MySQL数据库

如果我执行
ini\u集('memory\u limit','512M')
,PHP将耗尽内存,但如果执行
ini\u集('memory\u limit','1024M')
,PHP将成功

为什么这个脚本占用了这么多内存?一个由40000个元素组成的数组,每个元素不超过20个字符左右,应该只占用几MB的内存。我没有对数组进行任何处理

编辑:这是我的代码

mssql_query("SET ANSI_WARNINGS ON");
mssql_query("SET ANSI_NULLS ON");

ini_set('memory_limit', '1024M');
$cbsa='16980';
if(isset($_POST['cbsa'])){
    $cbsa = sanitize($_POST['cbsa']);
}

$zipstrings = get_zipstring($cbsa); //external function that returns two lists of about 500 zipcodes each

$lat_lng = array();
foreach($zipstrings as $zipstring){
  $result = mssql_query("select * from openquery(database, 'SELECT lat, lng from coordinates 
  JOIN with other table....");
  while($row = mssql_fetch_array($result, MSSQL_ASSOC)){
      $lat_lng[] = array("lat"=>$row['lat'], "lng"=>$row['lng']); **CRASHES HERE**
  }
}
echo json_encode(array("coords"=>$lat_lng, "error"=>mssql_get_last_message()));

该查询返回多少数据?如果有一个select*from opendata,它有非常大的列,其中许多列都是原因。您的代码在我看来正常

我们无法使用code@user20232359723568423357842364你为什么不改变你的用户名呢?斯科特W:你能发布你的代码吗?你可能在某个地方卡住了一个环。。这种大小的40k个元素只有2MB。你能发布一些代码吗?我添加了代码,它在while循环中崩溃了。我建议添加一些对
mssql\u free\u语句
mssql\u free\u结果
的调用,它们将释放查询中使用的内存。您的代码将为每个zip运行一个查询,在完成整个脚本之前可能不会再次关闭查询。我只选择两列(lat,lng)。select*是因为我必须通过MSSQL查询MYSQL数据库,所以查询基本上是说“select*from(select lat,lng)”为什么要抽象?为什么不直接查询?由于权限和安全问题超出了我的控制范围,我无法直接查询MySQL数据库,但必须通过MSSQL和“链接服务器”进行查询。在没有内部循环的情况下尝试,比如400 mb内存。看看是否有问题