0

I am starting to learn PHP and will need to connect to SQL Server. The web server I am using is Apache. My PHP below outputs the "Connection established" in the browser window okay but to make sure I am getting data too I included the code for getting a row count but for some reason the code around the row count doesn't appear to work but yet the code after the row count part outputs the "Results returned" to the browser. I have tried changing the SELECT to a count using COUNT(Name) and also changed

$result = sqlsrv_query($conn, $query)

to

$result = sqlsrv_query($conn, $query, array(), array("Scrollable" => 
'static'))

But neither has worked so far. Why is the row count not working?

<!DOCTYPE html>
<html>
 <head>
  <meta charset="UTF-8">
  <title>Check SQL Server Connection</title>
 </head>
 <body>
   <?php
     $serverName = "IT90334\SQLEXPRESS";
     $connectionInfo = array('Database' => 'AdventureWorks');
     $conn = sqlsrv_connect($serverName, $connectionInfo);

     if ($conn) {
       echo "Connection Established.<br />";
     } else {
       echo "Something went wrong while connecting to MSSQL.<br />";
       die(print_r(sqlsrv_errors(), true));
     }

     $query = "SELECT Name FROM production.Location"
     $result = sqlsrv_query($conn, $query)
        or die('An error has occurred');

     $rowcount = sqlsrv_num_rows($result);

     if($rowcount === FALSE){
       echo "failure";
     }
     else {
       echo $rowcount;
     }

     if($result === FALSE){
       die(print_r(sqlsrv_errors(), true));
     }
     else {
       echo "Results returned. <br />;
     }

     sqlsrv_close($conn);

  ?>
  </body>
</html> 
mjhenry
  • 53
  • 1
  • 14

2 Answers2

0

You have a couple of syntax issues here:

<!DOCTYPE html>
<html>
 <head>
  <meta charset="UTF-8">
  <title>Check SQL Server Connection</title>
 </head>
 <body>
   <?php
     $serverName = "IT90334\SQLEXPRESS";
     $connectionInfo = array('Database' => 'AdventureWorks');
     $conn = sqlsrv_connect($serverName, $connectionInfo);

     if ($conn) {
       echo "Connection Established.<br />";
     } else {
       echo "Something went wrong while connecting to MSSQL.<br />";
       die(print_r(sqlsrv_errors(), true));
     }

     $query = "SELECT Name FROM production.Location"; -- HERE
     $result = sqlsrv_query($conn, $query)
        or die('An error has occurred');

     $rowcount = sqlsrv_num_rows($result);

     if($rowcount === FALSE){
       echo "failure";
     }
     else {
       echo $rowcount;
     }

     if($result === FALSE){
       die(print_r(sqlsrv_errors(), true));
     }
     else {
       echo "Results returned. <br />"; -- HERE
     }

     sqlsrv_close($conn);

  ?>
  </body>
</html> 

Remember to always terminate your lines with a semi-colon, and close your strings with double/single quotes (preferably double quotes and nest single quotes inside).

John Bell
  • 2,350
  • 1
  • 14
  • 23
  • Thanks. I corrected my syntax errors and the outcome is the same – mjhenry Dec 17 '15 at 15:40
  • @mjhenry If you couldn't tell that those errors were there, that presumably means you have error reporting turned off, which would be a bad thing when you're trying to track down a problem. [Turn on all the error reporting you can](http://stackoverflow.com/questions/845021/how-to-get-useful-error-messages-in-php). – Matt Gibson Dec 17 '15 at 22:18
0

Found that in my code one of the $rowcount variables was actually $rowCount (upper case C). This made the code work and output the failure. But then I changed the setting of the $result to have the scrollable static parameters again as shown here and it worked

$result = sqlsrv_query($conn, $query, array(), array("Scrollable" => 
'static'))
mjhenry
  • 53
  • 1
  • 14