sql server data to json using php

时间:2023-02-10 23:47:38

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

我的json代码没有显示任何内容,我已经尝试了很多代码,但没有任何帮助。

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);

3 个解决方案

#1


1  

There are numerous likely issues with this:

这有很多可能的问题:

1) Have you checked your query actually returns rows?

1)您是否检查过您的查询实际返回行?

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

2)你将数据循环两次(两次($ row = sqlsrv_fetch_array ...循环),这是无效或有效的。

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

3)do ... while(sqlsrv_next_result($ stmt));子句也应该是不必要的,因为fetch_array会知道它何时到达数据的末尾,并且你只有一个结果集,所以你不需要在它们之间移动

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

4)您正在回显原始数据和JSON,因此如果您对此脚本进行ajax调用,它将失败,因为响应将部分包含非JSON数据

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);

#2


0  

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:

正如ADyson所说:

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:

for double check在此代码中添加echo,如下所示:

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

如果此代码有效,请接受ADyson答案

#3


0  

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;

#1


1  

There are numerous likely issues with this:

这有很多可能的问题:

1) Have you checked your query actually returns rows?

1)您是否检查过您的查询实际返回行?

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

2)你将数据循环两次(两次($ row = sqlsrv_fetch_array ...循环),这是无效或有效的。

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

3)do ... while(sqlsrv_next_result($ stmt));子句也应该是不必要的,因为fetch_array会知道它何时到达数据的末尾,并且你只有一个结果集,所以你不需要在它们之间移动

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

4)您正在回显原始数据和JSON,因此如果您对此脚本进行ajax调用,它将失败,因为响应将部分包含非JSON数据

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);

#2


0  

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:

正如ADyson所说:

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:

for double check在此代码中添加echo,如下所示:

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

如果此代码有效,请接受ADyson答案

#3


0  

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;