Jump to content

Trying to connect to SQL Server DB


Go to solution Solved by requinix,

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


   $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

This thread is more than a year old.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • 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.