使用php将sql server数据转换为json
我的json代码没有显示任何内容,我已经尝试了许多代码,但没有任何帮助使用php将sql server数据转换为json,php,sql,json,sql-server,sqlsrv,Php,Sql,Json,Sql Server,Sqlsrv,我的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));
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);
include('connect.php');
$sql=“从项目中选择*”;
$stmt=sqlsrv_查询($conn,$sql);
如果($stmt==false)
{
echo“查询准备/执行中出错。\n”;
模具(打印错误(sqlsrv_errors(),true));
}
while($row=sqlsrv\u fetch\u array($stmt,sqlsrv\u fetch\u ASSOC))//此循环正在工作
{
echo$row['item_id']。”、“$row['item_name']。”、“$row['Barcode']。”
”;
}
$json=array();
做{
而($row=sqlsrv\u fetch\u数组($stmt,sqlsrv\u fetch\u ASSOC)){
$json[]=$row;
}
}while(sqlsrv_next_result($stmt));
echo json_encode($json)//空的?!
sqlsrv_free_stmt($stmt);
这方面可能存在许多问题:
1) 您是否检查了查询是否实际返回了行
2) 您将数据循环两次(两次而($row=sqlsrv\u fetch\u array…
循环),这既没有用处也没有效率
3) do…while(sqlsrv_next_result($stmt))代码>子句也应该是不必要的,因为fetch_数组将知道何时到达数据的末尾,并且您只有一个resultset,所以不需要在它们之间移动
4) 您正在回显原始数据和JSON,因此如果您对此脚本进行ajax调用,它将失败,因为响应将部分包含非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));
}
$json = array();
while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC))
{
$json[] = $row;
}
echo json_encode($json);
如果此操作有效:
while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC)) //this loop is working
{
echo $row['item_id'].", ".$row['item_name'].", ".$row['Barcode']."<br>";
}
要进行双重检查,请在此代码中添加回音,如下所示:
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($stmt==false)
{
echo“查询准备/执行中出错。\n”;
模具(打印错误(sqlsrv_errors(),true));
}
$json=array();
而($row=sqlsrv\u fetch\u数组($stmt,sqlsrv\u fetch\u ASSOC))
{
echo$row['item_id']。”、“$row['item_name']。”、“$row['Barcode']。”
”;
$json[]=$row;
}
echo json_encode($json);
如果此代码有效,请接受ADyson的答案以下是解决此问题的方法。希望您的查询编写良好
$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;
$dataFinal=array()//包含json总数组的最终变量
对于($k=0;$k$行['item_id'],
“项目名称”=>$row['item\u name'],“条形码”=>$row['Barcode']);
阵列推送($dataFinal,$ligne)//在数组datafinal中添加行
}//循环结束
$result=数组($dataFinal);
$response=新响应(json_encode($dataFinal));
$response->headers->set('Content-Type','application/json');
返回$response;
移动$json=array()代码>在第一个while循环之前重置。每次迭代都要重置数组。@aynber将代码格式化得更好,我认为这不是问题所在issue@OscarZarrus把代码格式化得更好后,我认为这不是问题所在issue@ADyson抢手货格式化使事情更容易阅读。对,我正要回答同样的问题。有一个奇怪的嵌套循环和数组重置每个语句+1我的查询正在工作,我尝试了你的代码,但它没有显示任何内容。json仍然有效empty@EnasAK为了进行测试,在循环结束后,编写var\u dump($json)代码>它是否在输出中显示一些结果?如果有,那么你肯定有一些数据。在这种情况下,唯一合乎逻辑的可能性是json_编码操作失败。您可以很容易地测试它,因为您可以检查它是否返回false,并查看错误是什么:write$encodedJSON=json\u encode($json);如果($encodedJSON==false)回显json_last_error_msg();否则,埃克森代码>。请参阅(并且json\u last\u error()
将获得错误ID)这是一个谜。您能在每次交互中转储$row变量吗?结果如何?我们必须叫个驱魔师。如果$row是一个数组,并且echo$row['item_id']打印了一些内容,那么必须填充$json变量。
$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;