Jump to content

Trying to connect to SQL Server DB


alanl1

Recommended Posts

Hi professionals

 

Fairly new to php, anyway I have installed php successfully i think and displayed a hello world.

 

now I am trying to connect to our Sql Server 2008 database but it keeps throwing the error "Error connecting to SQL Server"

 

 

any ideas where I could be going wrong, could it be the install, do i need to allow sql server to let webpages connect to the db etc I honestly have no idea.

 

here is the code

 

<?php
   $serverName = "d3licsql02";
   $database = "TestData";

   // Get UID and PWD from variables.
   $uid = "sa";
   $pwd = "secret";

echo $serverName;
echo "<br>";
echo $database;
echo "<br>";
echo $uid;
echo "<br>";
echo $pwd;
echo "<br>";

   try {
      $conn = new PDO( "sqlsrv:server=$serverName;Database = $database", $uid, $pwd);
      $conn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
   }

   catch( PDOException $e ) {
      die( "Error connecting to SQL Server" );
   }

   echo "Connected to SQL Server\n";

   $query = 'select * from Postcodes.Suburb';
   $stmt = $conn->query( $query );
   while ( $row = $stmt->fetch( PDO::FETCH_ASSOC ) ){
      print_r( $row );
   }

   // Free statement and connection resources.
   $stmt = null;
   $conn = null;
?>

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.