简体   繁体   中英

sql server data to json using php

My json code doesn't display anything, I have already tried many codes but nothing has helped.

include('connect.php');
$sql = "SELECT *  FROM items";  
$stmt = sqlsrv_query( $conn, $sql);

if( $stmt === false)
{
   echo "Error in query preparation/execution.\n";  
   die( print_r( sqlsrv_errors(), true));  
}

while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC))  //this loop is working
{    
  echo $row['item_id'].", ".$row['item_name'].", ".$row['Barcode']."<br>"; 
}

$json = array();
do {
   while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {
     $json[] = $row;
   }
} while ( sqlsrv_next_result($stmt) );

echo json_encode($json);  //empty?!
sqlsrv_free_stmt( $stmt);

There are numerous likely issues with this:

1) Have you checked your query actually returns rows?

2) You're looping your data twice (two while( $row = sqlsrv_fetch_array... loops) which is not useful or efficient.

3) the do...while ( sqlsrv_next_result($stmt) ); clause should be unnecessary as well, as fetch_array will know when it's got to the end of the data, and you've only got one resultset, so you don't need to move between them

4) you're echoing raw data as well as JSON, so if you make an ajax call to this script it'll fail because the response will partly contain non-JSON data

I think this will be sufficient to get you some sensible data:

include('connect.php');
$sql = "SELECT *  FROM items";  
$stmt = sqlsrv_query( $conn, $sql);

if( $stmt === false)
{
   echo "Error in query preparation/execution.\n";  
   die( print_r( sqlsrv_errors(), true));  
}

$json = array();

while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC))
{    
     $json[] = $row;
}

echo json_encode($json);

If THIS works:

while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC))  //this loop is working
{    
  echo $row['item_id'].", ".$row['item_name'].", ".$row['Barcode']."<br>"; 
}

the rest must work too.

As ADyson says:

if( $stmt === false)
{
   echo "Error in query preparation/execution.\n";  
   die( print_r( sqlsrv_errors(), true));  
}

$json = array();

while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC))
{    
     $json[] = $row;
}

echo json_encode($json);

for double check add your echo in this code, like this:

if( $stmt === false)
{
   echo "Error in query preparation/execution.\n";  
   die( print_r( sqlsrv_errors(), true));  
}

$json = array();

while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC))
{    
     echo $row['item_id'].", ".$row['item_name'].", ".$row['Barcode']."<br>";
     $json[] = $row;
}

echo json_encode($json);

If this code works, accept the ADyson answer

here is a way to solve the issue .Hoping your query is well written.

$dataFinal= array();//final variable that will contain total array for json   
for($k=0;$k<count(variable_that_contents_the_resutl_array_query);$k++){

$ligne = array("item_id"=> $row['item_id'],
"item_name"=>$row['item_name'],"Barcode"=> $row['Barcode']); 
array_push($dataFinal, $ligne);//add line in array datafinal         

     }//end for loop
    $result = array($dataFinal);
    $response = new Response(json_encode($dataFinal));
    $response->headers->set('Content-Type', 'application/json');
    return $response;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM