Trying Out The SQL Server 2005 Driver For PHP

I regularly browse the ASP.NET Weblogs for hot tips so when Hosam Kamel and Jose R. Guay Paz announced the release of the SQL Server 2005 Driver for PHP I decided to give it a try.

The download includes a compiled help file SQLServerDriverForPHP.chm with installation instructions and sample code. I won't repeat anything you can already find there but below is a screen shot of a new section that appears when you call phpinfo:

I already had some sample code to export data from SQL Server to JSON using PHP so I just converted it to use this new driver:

   1: /* Specify the server and connection string attributes. */
   2: $serverName = "(local)";
   3: $connectionInfo = array( "UID"=>"sa",
   4:                          "PWD"=>"password",
   5:                          "Database"=>"Northwind");
   6:                          
   7: /* Connect using SQL Server Authentication. */
   8: $conn = sqlsrv_connect( $serverName, $connectionInfo);
   9: if( $conn === false )
  10: {
  11:      echo "Unable to connect.</br>";
  12:      die( print_r( sqlsrv_errors(), true));
  13: }
  14:  
  15: $tsql = "SELECT * FROM Customers";
  16: $stmt = sqlsrv_query( $conn, $tsql);
  17: if( $stmt === false )
  18: {
  19:      echo "Error in executing query.</br>";
  20:      die( print_r( sqlsrv_errors(), true));
  21: }
  22: $data = "["; 
  23: while($row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC))
  24: {
  25:     $data = $data . json_encode($row) . ", ";
  26: }    
  27: $data = $data . "]";   
  28: echo $data;
  29:  
  30: /* Free statement and connection resources. */
  31: sqlsrv_free_stmt( $stmt);
  32: sqlsrv_close( $conn);
NOTE: The code snippit plug-in does not support PHP code so you get no syntax highlighting.

No Comments