|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2008-06-17 11:13 UTC] lowee at lowee dot net
Description:
------------
php cli with pdo and ms sql
it is working on any web browser but on php cli it is not working
Reproduce code:
---------------
define('DB_HOST','192.168.1.20');
# mtc_awdb
# mtc_hds
define('DB_NAME','db1');
define('DB_USER','user');
define('DB_PASS','pass');
$dbhandle = mssql_connect(DB_HOST, DB_USER, DB_PASS) or die("Couldn't connect to SQL Server on");
try {
$pdo = new PDO('mssql:host='.DB_HOST.';dbname='.DB_NAME,DB_USER,DB_PASS);
}catch (PDOException $e) {
print $e->getMessage();
}
Expected result:
----------------
to show me that it is connected
Actual result:
--------------
1 - SQLSTATE[01002] Unable to connect: SQL Server is unavailable or does not exist.
(severity 9)
2 - SERVER not available
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Mon Nov 03 18:00:01 2025 UTC |
Check what php.ini file is actually loaded. Most likely you're just missing the proper extensions. Then change that script a bit: <?php define('DB_HOST','192.168.1.20'); define('DB_NAME','db1'); define('DB_USER','user'); define('DB_PASS','pass'); try { $pdo = new PDO('mssql:host='.DB_HOST.'dbname='.DB_NAME,DB_USER,DB_PASS); }catch (PDOException $e) { print $e->getMessage(); } ?> And post the output here.Very similar problem here with pdo_dblib. Works fine under Apache module, but not CLI. I've tried to ensure everything was built correctly. <?php $user = 'foo'; $pass = 'bar'; $dbh = new PDO('dblib:host=odyssey:2638;dbname=odyssey', $user, $pass); ?> > php testpdo.php Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY000] Server is unavailable or does not exist. (severity 9)' in /usr/home/kian/public_html/testpdo.php:5 Stack trace: #0 /usr/home/kian/public_html/testpdo.php(5): PDO->__construct('dblib:host=odys...', 'foo', 'bar') #1 {main} thrown in /usr/home/kian/public_html/testpdo.php on line 5 > php -v PHP 5.2.6 with Suhosin-Patch 0.9.6.2 (cli) (built: Jul 22 2008 16:30:13) (DEBUG) Copyright (c) 1997-2008 The PHP Group Zend Engine v2.2.0, Copyright (c) 1998-2008 Zend Technologies FreeBSD 7.0.Same issue with me: Neither PDO lib is working nor MSSQL I have tried both 1) MSSQL: try { $pdo = new PDO ("mssql:host=".$servername.":".$port.";dbname=".$dbname,$username,$password); } catch (PDOException $e) { echo "Failed to get DB handle: " . $e->getMessage() . "\n"; exit; } Error is: Failed to get DB handle: could not find driver 2) PDO-lib try { $pdo = new PDO ("dblib:host=".$servername.":".$port.";dbname=".$dbname,$username,$password); } catch (PDOException $e) { echo "Failed to get DB handle: " . $e->getMessage() . "\n"; exit; } Error is: Failed to get DB handle: SQLSTATE[HY000] Server is unavailable or does not exist. (severity 9)