0

Code I'm using to test connection:

   $serverName = 'host\sqlexpress';
   $database = 'dbname';

   // Get UID and PWD from application-specific files.
   $uid = 'username';
   $pwd = 'password';

   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 tablename';
   $stmt = $conn->query( $query );
   while ( $row = $stmt->fetch( PDO::FETCH_ASSOC ) ){
      print_r( $row );
   }

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

The error I'm getting when running this file is just, "Error connecting to SQL Server"

Any clue why this may not be working? Any way I can get a more detailed error report?

hjpotter92
  • 78,589
  • 36
  • 144
  • 183
MikeOscarEcho
  • 535
  • 2
  • 12
  • 27
  • 5
    Check PDO's actual error, not your string message. `echo $e->getMessage()` inside the `catch{}` block. – Michael Berkowski Jan 21 '14 at 20:21
  • @MichaelBerkowski Okay, got a driver error. "could not find driver". Am I in the right area if I follow this tutorial: http://www.codesynthesis.com/~boris/blog/2011/12/02/microsoft-sql-server-odbc-driver-linux/ or do you recommend something else? – MikeOscarEcho Jan 21 '14 at 20:27
  • 1
    Looks like that tutorial is for ODBC, which is a different driver in PDO than sqlsrv. If that's a Debian-based system you are working with, do an `apt-cache search pdo` to see what drivers are available. I can't recall that I have ever connected PDO to MSSQL from Linux to know whether the sqlsrv driver or odbc is easier. – Michael Berkowski Jan 21 '14 at 20:31
  • There is some information for your problem: http://stackoverflow.com/questions/2852748/pdoexception-could-not-find-driver – Erik255 Jan 21 '14 at 20:31
  • Ahh okay, yea its debian based (ubuntu). Thanks for the help I think I know what I need to do @MichaelBerkowski – MikeOscarEcho Jan 21 '14 at 20:35
  • Why tag it as [tag:mysql]? – hjpotter92 Jan 22 '14 at 02:14
  • @hjpotter92 Sorry about that I meant to tag it as mssql – MikeOscarEcho Jan 22 '14 at 12:18

0 Answers0