无法在json php中获取巨大的amonut数据

时间:2016-06-29 10:35:41

标签: 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 [&#39; Retail_store_prod_com&#39;] = $ json_data49;         echo json_encode($ result,JSON_PRETTY_PRINT);         $ json_arr = json_encode($ result,JSON_PRETTY_PRINT);         // ------------------------------------------------ ----- sucessresponse -------------------------------------------- ------------------------

    ?> 

3 个答案:

答案 0 :(得分:2)

尝试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
)

答案 1 :(得分:0)

$cnt = 0;
$rows = array('John',123, 'Lloyds Office','Jane',124, 'Lloyds Office','Billy',125, 'London Office','Miranda', 126, 'Bristol Office');
$insertQry = "insert into MyTable ( Name,     Id,   Location)        values ";
foreach($rows as $row) {
  if($cnt == 3) {
     $insertQry = rtrim($insertQry ,", ");
     $insertQry .= "), ";
  }
  if($cnt > 2) {
    $cnt = 0;
  }
  if($cnt == 0) {
     $insertQry .= "(";
  } 

  $insertQry .= $row.", ";
  $cnt++;
}
$insertQry = rtrim($insertQry ,", ");
$insertQry .= ");";

输出:

insert into MyTable ( Name, Id, Location) values (John, 123, Lloyds Office), (Jane, 124, Lloyds Office), (Billy, 125, London Office), (Miranda, 126, Bristol Office);

答案 2 :(得分:0)

<?php

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

foreach ($arraychuk as $array_num => $array) {
  echo "Array $array_num:\n";
 }


?>

//你可以很容易地将其打成一个完美的阵列