Very Confusing PDO php exec() connection issue -
i have run confusing issue connecting database via php pdo using exec().
i have thrown following snippet illustrate point.
$host = "localhost"; $db_name = "some_db"; $user_name = "some_user"; $pass_word = "some_password"; try { // assign pdo object db variable $dbh = new pdo("mysql:host=$host;dbname=$db_name;charset=utf8", $user_name, $pass_word, array(pdo::mysql_attr_init_command => "set names 'utf8'")); $dbh->setattribute( pdo::attr_errmode, pdo::errmode_exception ); echo "yahoo connected"; } catch (pdoexception $e) { //output error - log error file rather output user. echo "connection error: " . $e->getmessage(); }
when run code via browser connects fine when run @ command line gives following error:
connection error: sqlstate[28000] [1045] access denied user 'some_user'@'localhost' (using password: no)
needless confusing password indeed set can see in code above , connection works , prints yahoo screen in browser. ideas appreciated.
your snippet wrong. ought without useless try-catch block:
$host = "localhost"; $db_name = "some_db"; $user_name = "some_user"; $pass_word = "some_password"; $dsn = "mysql:host=$host;dbname=$db_name;charset=utf8" $opt = array( pdo::attr_errmode => pdo::errmode_exception, pdo::mysql_attr_init_command => "set names utf8", // other options ); $dbh = new pdo($dsn, $user_name, $pass_word, $opt); echo "yahoo connected";
this way complete error message,including stack trace show actual file runs - in there empty password used.
it important have error_reporting(e_all);
tell possible issues variables scope
Comments
Post a Comment