php - Reference - frequently asked questions about PDO -
what this?
this list of asked questions regarding php data objects
why this?
as pdo has features unknown regular php user, questions regarding prepared statements , error handling in pdo quite frequent. so, place them can found.
what should here?
if question has been closevoted list, please find question below , apply fix code. idea take brief other questions, make prepared other common pitfalls.
the list
- pdo query fails can't see errors. how error message pdo?
- how can use prepared statements operator?
- how can create prepared statement in () operator?
- can use pdo prepared statement bind identifier (a table or field name) or syntax keyword?
- pdo prepared statement causes error in limit statement
see also
pdo query fails can't see errors. how error message pdo?
to able see database errors, 1 have set pdo errmode exceptions. exceptions better regular errors in many ways: contains stack trace, can caught using try..catch or handled using dedicated error handler. , unhandled, act regular php errors providing important information, following site-wide error reporting settings.
note setting mode connection option let pdo throw exceptions on connection errors too, important.
so, here example creating pdo connection right way:
$dsn = "mysql:host=$host;dbname=$db;charset=utf8"; $opt = array( pdo::attr_errmode => pdo::errmode_exception, // other options ); $pdo = new pdo($dsn, $user, $pass, $opt);
connecting way, notified of database errors, occurred during query execution. note have able see php errors in general. on live site have peek error logs, so, settings have be
error_reporting(e_all); ini_set('display_errors',0); ini_set('log_errors',1);
while on local development server it's ok make errors on screen:
error_reporting(e_all); ini_set('display_errors',1);
and of course should never ever use error suppression operator (@
) in front of pdo statements.
also, due many bad examples telling wrap every pdo statement try..catch
block, have make distinct note:
do not use try..catch operator echo error message. uncaught exception excellent purpose, act same way other php errors - so, can define behavior using site-wide settings - so, you have error message without useless code. while unconditionally echoed error message may reveal sensitive information potential attacker, yet confuse honest visitor.
- a custom exception handler added later, not required. new users, recommended use unhandled exceptions, extremely informative, helpful , secure.
- use
try..catch
if going handle error - say, rollback transaction.
Comments
Post a Comment