0

I'm trying to establish a connection with PHP and MS SQL Server 2005. I'm using IIS 8 in Windows 8 and PHP version 5.3.28. I already done my research, I've found MSSQL PHP PDO (http://www.php.net/manual/en/ref.pdo-sqlsrv.connection.php and http://technet.microsoft.com/en-us/library/ff754357(v=sql.105).aspx), downloaded and enabled all of PDO related DLLs I've found from Microsoft website (Microsoft Drivers 3.0 for PHP for SQL Server) and assured they are reflecting in phpinfo().

extension=php_pdo_sqlsrv_52_nts_vc6.dll
extension=php_pdo_sqlsrv_53_nts.dll
extension=php_pdo_sqlsrv_53_nts_vc6.dll
extension=php_pdo_sqlsrv_53_nts_vc9.dll
extension=php_pdo_sqlsrv_54_nts.dll
extension=php_pdo_sqlsrv_54_nts.dll
extension=php_sqlsrv_54_nts.dll
extension=php_sqlsrv_53_ts_vc9.dll

The following links in Stock Over Flow also helped me alot in my dilemma:

I'm able to fix the "driver not found" and "connection string is invalid" issues, and here's my current PHP codes:

$db_server = "func.website.com,8787";
//$db_server = "func.website.com:8787\sqlexpress";
$db_database = "db_BreadNButter";
$db_user = "tinapay";
$db_passwd = "p@ssword";

try {
   //$db = new PDO ("mssql:host=$db_server;dbname=$db_database;", $db_user, $db_passwd);
   $db = new PDO ("sqlsrv:Server=$db_server;Database=$db_database;", $db_user, $db_passwd);
} catch(PDOException $exception) {
   die("Unable to open database.<br>Error message:<br><br>$exception.");
}

I think (fingers-crossed) that I already established a connection to MSSQL DBase, but I'm getting the error below:

exception 'PDOException' with message 'SQLSTATE[08001]:
[Microsoft][SQL Server Native Client 11.0]TCP Provider: Timeout error [258].
' in C:\inetpub\wwwroot\breadnbutter\class.connection.php:12 Stack trace: #0
C:\inetpub\wwwroot\breadnbutter\class.connection.php(12):
PDO->__construct('sqlsrv:Server=f...', 'tinapay', 'p@ssword') #1 {main}.

Can anyone enlighten me on why am I getting timeout errors and how can I fix this?

Community
  • 1
  • 1
thedevgypsy
  • 79
  • 1
  • 13
  • from my experience trying to connect to MSSQL I ended up using php_odbc - if you don't have to use PDO maybe try that – Darius Mar 28 '14 at 09:19
  • Thanks for the reply. I'm not allowed to use ODBC... It's a shame though... I know it's much easier if I do that. – thedevgypsy Mar 28 '14 at 09:20
  • also I remember that I couldn't use prefix dbo_ before table names - I was pulling my hair out until found out about that - after I removed that everything worked fine – Darius Mar 28 '14 at 09:25
  • Anyways, I really need help in making this work. I really do appreciate those people helping me out here. I'm still kinda stuck... – thedevgypsy Mar 28 '14 at 10:00
  • I've tried to revised my code and tried to display something: `try { $dbh = new PDO ("sqlsrv:Server=$db_server;Database=$db_database;", $db_user, $db_passwd); foreach($dbh->query('SELECT * from dbo.BGCountry') as $row) { print_r($row); } $dbh = null; } catch (PDOException $e) { print "Error!: " . $e->getMessage() . "
    "; die(); }` I'm still getting the same error: `SQLSTATE[08001]: [Microsoft][SQL Server Native Client 11.0]TCP Provider: Timeout error [258]`
    – thedevgypsy Mar 28 '14 at 11:02
  • try to isolate the code and debug in smaller chunks; dump `$dbh` and see if you get PDO object; from my experience `SQLSTATE[08001` is quite broad in terms of when it gets thrown – Darius Mar 28 '14 at 11:06
  • also I see that you use `dbo` prefix before table name - try without it as this is where I got stuck and after removing this it worked fine – Darius Mar 28 '14 at 11:07
  • how should i dump it? – thedevgypsy Mar 28 '14 at 11:45
  • `var_dump`, `print_r` or `var_export` whatever suit your needs - you need to check what is in `$dbh` after PDO constructor has been invoked – Darius Mar 28 '14 at 11:49

3 Answers3

1

The port should be func.website.com:8787. You have a typo with the comma.

Meliz
  • 21
  • 3
  • Thanks for the reply, I tried that already and that gives me the error `[Microsoft][SQL Server Native Client 11.0]SQL Server Network Interfaces: Connection string is not valid [87]` – that's why I used comma(,) instead of colon (:) – thedevgypsy Mar 28 '14 at 09:29
1

how to connect sqlsrv 2008 in php 5.4.4 erro is: Array ( [0] => Array ( [0] => IMSSP [SQLSTATE] => IMSSP [1] => -49 [code] => -49 [2] => This extension requires the Microsoft SQL Server 2012 Native Client. Access the following URL to download the Microsoft SQL Server 2012 Native Client ODBC driver for x86: http://go.microsoft.com/fwlink/?LinkId=163712 [message] => This extension requires the Microsoft SQL Server 2012 Native Client. Access the following URL to download the Microsoft SQL Server 2012 Native Client ODBC driver for x86: http://go.microsoft.com/fwlink/?LinkId=163712 )

[1] => Array
    (
        [0] => IM002
        [SQLSTATE] => IM002
        [1] => 0
        [code] => 0
        [2] => [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified
        [message] => [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified
    )

)

0

I'm still having problems with this, but I'm able to convince my boss to use asp instead of PHP. thanks a lot for those who helped me out.

thedevgypsy
  • 79
  • 1
  • 13