Question

I just setup a PHP server using IIS and a Microsoft SQL server, both running on the same Windows Server 2008 machine. I installed the SQLSRV driver from Microsoft on PHP, because I want to use my SQL Server database in PHP.

Connecting to the SQL Server instance runs fine in the code but when I try to execute queries, it doesn't work anymore. The same queries I try to run from the php code are working in other programs, such as my SQL Server Manager. Even simple commands like 'USE dbname' aren't working, while they do work from SQL Server Manager.

This is the PHP code that I'm using. When I run the code it says connected and after that Error Selecting DB.

$conn = sqlsrv_connect("TRANSIP-VPS\SQLEXPRESS");
if( $conn === false )
  { die( FormatErrors( sqlsrv_errors() ) ); }
echo "Connected ";
      $selectDB = sqlsrv_query( $conn, "USE Vitare");
      if($selectDB === false){
        die("ERror selecting DB");  
      }

$tsql = "CREATE TABLE Persons
(
PersonID int,
LastName varchar(255),
FirstName varchar(255),
Address varchar(255),
City varchar(255)
)";
  $getProducts = sqlsrv_query( $conn, $tsql);
  if ( $getProducts === false)
        {
        echo "Error creating table";
        die( FormatErrors( sqlsrv_errors() ) ); }
echo "stuff";
$row = sqlsrv_fetch_array( $getProducts, SQLSRV_FETCH_ASSOC);
echo $row['username'];

When I run sql_srverrors() instead of printing 'error selecting DB' , it only gives me the word 'Array', nothing more, no error message, nothing.

Was it helpful?

Solution

Try this to connect to the database:

$serverName = "TRANSIP-VPS\SQLEXPRESS";
$connectionInfo = array( "Database"=>"Vitare" );
$conn = sqlsrv_connect( $serverName, $connectionInfo);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top