PHP错误sqlsrv_fetch_array()

时间:2014-09-18 23:47:58

标签: php

我收到此错误,尝试解决但无法找到问题:

  

ERROR“警告:sqlsrv_fetch_array()要求参数1为资源,第14行的C:\ xampp \ htdocs \ test3.php中给出布尔值

来源:

 <?php

   $serverName ="12.10.12.120";  $usr="myuser";  $pwd="myuser1"; 
   $db="Mydabb";

   $connectionInfo = array("UID" => $usr, "PWD" => $pwd, "Database" =>
   $db);

   $conn = sqlsrv_connect($serverName, $connectionInfo);

   $sql = "SELECT Name, Address, Amount  FROM Order "; $res =
   sqlsrv_query($conn, $sql); while ($row = sqlsrv_fetch_array($res))  
   {
       print($row['Name'].",".$row['Address'].",".$row['Amount']);  }  

   ?>

任何帮助将不胜感激

2 个答案:

答案 0 :(得分:1)

ORDER是SQL和MSSQL reserved word,必须用方括号括起来。

更改此行

$sql = "SELECT Name, Address, Amount  FROM Order ";

$sql = "SELECT Name, Address, Amount  FROM [Order] ";

或更改其名称,如果可能,请更改为“ORDERS”。

如果发出错误信号,则应启用错误报告。


如评论中所述,请

if (!$res) die( print_r( sqlsrv_errors(), true));
sqlsrv_query

之后

答案 1 :(得分:0)

查询本身可能存在问题,因为函数sqlsrv_query在错误上返回布尔值。

来自以下文件:

http://php.net/manual/en/function.sqlsrv-fetch-array.php

我们看到在获取数组之前,示例首先检查sqlsrv_query的返回值是否等于false,如果是,那么它将使用sqlsrv_errors函数打印错误并退出。

您需要实现它以查看错误是什么,以便您可以进一步调试。

 <?php

   $serverName ="12.10.12.120";  $usr="myuser";  $pwd="myuser1"; 
   $db="Mydabb";

   $connectionInfo = array("UID" => $usr, "PWD" => $pwd, "Database" =>
   $db);

   $conn = sqlsrv_connect($serverName, $connectionInfo);

   $sql = "SELECT Name, Address, Amount  FROM Order "; 

   $res = sqlsrv_query($conn, $sql); 

   if( $res === false) {
     die( print_r( sqlsrv_errors(), true) );
   }

   while ($row = sqlsrv_fetch_array($res))  
   {
       print($row['Name'].",".$row['Address'].",".$row['Amount']);  }  

   ?>