Jump to content


Trying to connect to SQL Server DB

Best Answer requinix, 30 April 2013 - 01:29 AM

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

  • Members
  • PipPipPip
  • Advanced Member
  • 81 posts

Posted 30 April 2013 - 12:14 AM

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;

#2 requinix

  • Administrators
  • Lazy Administrator
  • 9,254 posts
  • LocationWA

Posted 30 April 2013 - 01:29 AM   Best Answer

Did you take a look at the error message in the $e exception?
The Reimann Zeta Function Trolley Problem | "Summer is when I, the great ice fairy, can show my true power!"

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users