alanl1 Posted April 30, 2013 Share Posted April 30, 2013 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 https://forums.phpfreaks.com/topic/277430-trying-to-connect-to-sql-server-db/ Share on other sites More sharing options...
requinix Posted April 30, 2013 Share Posted April 30, 2013 Did you take a look at the error message in the $e exception? Link to comment https://forums.phpfreaks.com/topic/277430-trying-to-connect-to-sql-server-db/#findComment-1427207 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.