Jump to content


Photo

Trying to connect to SQL Server DB


Best Answer requinix, 29 April 2013 - 08:29 PM

Did you take a look at the error message in the $e exception? Go to the full post


  • Please log in to reply
1 reply to this topic

#1 alanl1

alanl1

    Advanced Member

  • Members
  • PipPipPip
  • 81 posts

Posted 29 April 2013 - 07:14 PM

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;
?>



#2 requinix

requinix

    Transforming Moderator

  • Moderators
  • 6,124 posts
  • LocationWA

Posted 29 April 2013 - 08:29 PM   Best Answer

Did you take a look at the error message in the $e exception?




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com