0

I got this error, try to solve it but can not find the problem:

ERROR "Warning: sqlsrv_fetch_array() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\test3.php on line 14

SOURCE:

 <?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']);  }  

   ?>

Any help will be appreciated

4
  • after sqlsrv_query add if (!$res) die( print_r( sqlsrv_errors(), true)); Commented Sep 18, 2014 at 23:51
  • ORDER is a SQL reserved word. Use [ORDER] (edit). Commented Sep 18, 2014 at 23:54
  • @Fred-ii- it is MS sql, not mysql, in MS sql you should use [ instead of backticks Commented Sep 18, 2014 at 23:56
  • @Lashane I stand corrected. Commented Sep 18, 2014 at 23:57

2 Answers 2

1

ORDER is a SQL and MSSQL reserved word and must be wrapped in square brackets.

Change this line

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

to

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

or change the name for it, to "ORDERS" if possible.

You should enable error reporting, if would have signaled an error.


As stated in comments, place

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

after sqlsrv_query

Sign up to request clarification or add additional context in comments.

Comments

0

There is probably an issue with the query itself as the function sqlsrv_query returns a boolean on an error.

From the documentation at:

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

We see that before fetching the array the example first checks to see the return value of sqlsrv_query is equal to false and if it is then it will print the error with the sqlsrv_errors function and exit.

You will need to implement that to see what the error is so you can further debug.

 <?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']);  }  

   ?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.