php - Database connection could not be established; Invalid data source name (Only with PDO) -
i'm trying reach new pgsql database php. problem recieve error given in title. when try connect direct. works, when try connect pdo, gives error.
what checked:
- php.ini. required extensions uncommented (both php_pgsql.dll , php_pdo_pgsql.dll)
- made sure these 2 files in php folder.
made sure database,host,user,pass , port correct using simple script (shown belown)
this config file setting databases (i'm using 6 databases correctly. know fault not in file)
"aeges":{ "pdo_driver":"odbc", "user":"xxxx", "password":"xxxx", "database":"test", "host":"localhost", "port":1233 }, "postgre":{ "pdo_driver":"pgsql", "user":"xxxx", "password":"xxxx", "database":"asn", "host":"localhost", "port":5432 } etc..
this function retrieve information. cleaned bit make clear
public static function findavailableasn() { $postgre = db\factory::getinstance()->create('postgre'); $stmt = $postgre->prepare("select * asnregel.asnregel"); if ($stmt->execute()) { return $stmt->fetchall(\pdo::fetch_assoc); } /* $connect = pg_connect("host=localhost port=5432 dbname=asn user=xxxx password=xxxx"); if(!$connect) { die("error in connection: ".pg_last_error()); } $sql = "select * asnregel.asnregel"; $result = pg_query($connect,$sql); if(!$result) { die("error in sql query: ".pg_last_error()); } while($row = pg_fetch_array($result)) { echo $row[0].'<br/>'; echo $row[1].'<br/>'; echo $row[2].'<br/>'; echo $row[3].'<br/>'; } pg_free_result($result); pg_close($connect); */ }
so problem first part doesn't work (the part pdo). while commented (the part without pdo) does. kind of weird right?
somebody can me this?
invalid data source name
the error message pretty clear. data source name dsn string using in pdo constructor. , appears invalid.
thus, no need check php.ini, php_pgsql.dll or password - these errors have distinct error messages deal with.
so, need correct dsn string. print out current dsn , compare canonical one, right manual page. example, pgsql correct dsn format like
pgsql:host=localhost;port=5432;dbname=testdb;user=bruce;password=mypass
while pther drivers right format can found in manual
Comments
Post a Comment