PHP脚本无法以json格式查看查询结果,不会显示错误

时间:2016-11-09 16:58:17

标签: php sql-server json database

我试图从在线托管的MSSQL数据库中获取表的值,但是,我无法从中获取任何数据,我已经在代码中多次查看并且无法在代码中找到错误或错误类型代码。

我得到两个回声确认连接和查询已执行但之后我应该能够看到json,但没有显示任何内容,我确信查询是正确的,因为我在其他地方使用它。

注意:

  • 我有sqlsrv PHP扩展

脚本:

    <?php
error_reporting(1);

$serverName = "...";

/* Get UID and PWD from application-specific files.  */
$uid = '...';
$pwd = '...';
$connectionInfo = array( "UID"=>$uid,
                         "PWD"=>$pwd,
                         "Database"=>"programaplo"
                         );

/* Connect using SQL Server Authentication. */
$conn = sqlsrv_connect( $serverName, $connectionInfo);
if( $conn === false )
{
     echo "Unable to connect.</br>";
     die( print_r( sqlsrv_errors(), true));
}

echo "Conexão: sucesso \n";

$tsql = "SELECT * FROM Obras";
$stmt = sqlsrv_query($conn, $tsql);

if( $stmt === false ) {
     echo "Error in executing query.</br>";
     die( print_r( sqlsrv_errors(), true));
}

echo "Query: sucesso \n";

$json = array();

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

/* Run the tabular results through json_encode() */
/* And ensure numbers don't get cast to trings */
echo json_encode($json);
/* Free statement and connection resources. */
sqlsrv_free_stmt( $stmt);
sqlsrv_close( $conn);

?>

0 个答案:

没有答案