php - 无法在 json php 中获取大量数据

标签 php mysql json

    <?php 
i am using limit suppose 2000 it is fetch data but without using limit it is not fetching data   
    $db=new PDO('mysql:dbname="";host=;','','',array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'utf8'"));

    $str_id=$_POST['Store_Id'];    
    $row=$db->prepare("select  Store_Id from store where Store_Id='$str_id'");  
    $row->execute();//execute the query
     header("content-type:application/json");  
    $json_data=array();//create the array  
    foreach($row as $rec)//foreacc loop  
    {      
    $Store_id=$rec['Store_Id']; 



    array_push($json_data,$json_array);   
    } 
     header("content-type:application/json"); 



    //-------------------------------------------------------------------------------------retail_str_po_detail_hold-----------------------------------------------------------------//                     

    $row49=$db->prepare("SELECT * FROM storeprod where S_Flag='0' and Store_Id='$Store_id'");    
    $row49->setFetchMode(PDO::FETCH_ASSOC);

    $row49->execute();//execute the query  
    $json_data49=array();//create the array 

    $json_arr=array();
    while($record49=$row49->fetch())
    {       
    foreach($record49 as $rec49)//foreacc loop  
    {    
    $json_array49['field']=$rec49;  
    $array_push($json_data49,$json_array49);
    }
    }

我正在使用限制假设 2000 它是获取数据但不使用限制它不是获取数据
$result['Retail_store_prod_com'] =$json_data49; echo json_encode($result,JSON_PRETTY_PRINT); $json_arr = json_encode($result,JSON_PRETTY_PRINT); //-------------------------------------------- -----成功响应-------------------------------------------- ------------------------

    ?> 

最佳答案

试试array_chunk,希望对你有帮助

<?php

$row= array('John',123, 'Lloyds Office','Jane',124, 'Lloyds Office','Billy',125, 'London Office','Miranda', 126, 'Bristol Office');
$arraychuk = array_chunk($row, 3);


for($i=0;$i<count($arraychuk);$i++){

    echo "<pre>".print_r($arraychuk[$i]);

    insert into MyTable ( Name,     Id,   Location)
             values ($arraychuk[$i][0],   $arraychuk[$i][1], $arraychuk[$i][2]));


}

在使用array_chunk时你会得到这样的结果

Array
(
    [0] => Jane
    [1] => 124
    [2] => Lloyds Office
)
Array
(
    [0] => Billy
    [1] => 125
    [2] => London Office
)
Array
(
    [0] => Miranda
    [1] => 126
    [2] => Bristol Office
)

关于php - 无法在 json php 中获取大量数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38097102/

相关文章:

php - 从mysql的两个表中检索数据

sql - 在 MySQL 中选择查询

mysql - 计数未返回正确的值

php - Mysql 查询未运行或条件

java - JSON解析错误: Cannot deserialize instance of `` out of START_ARRAY token

php - 如何从 php 脚本运行 linux 命令来编译 c 程序

php - Doctrine2 DBAL 的良好替代 DBAL

php - highstock 的增量 MySQL 时间戳转换

javascript - JSON响应上的重复 block

json - 为什么我的 jq 在 JSON 上失败?