php - My PDO Statement doesn't work -
this php sql statement , it's returning false while var dumping
$password_md5 = md5($_get['password']); $sql = $dbh->prepare('insert users(full_name, e_mail, username, password, password_plain) values (:fullname, :email, :username, :password, :password_plain)'); $result = $sql->execute(array( ':fullname' => $_get['fullname'], ':email' => $_get['email'], ':username' => $_get['username'], ':password' => $password_md5, ':password_plain' => $_get['password']));
if pdo statement returns false
, means query failed. have set pdo in proper error reporting mode aware of error.
put line in code right after connect
$dbh->setattribute( pdo::attr_errmode, pdo::errmode_exception );
after getting error message, have read , comprehend it. sounds obvious, learners overlook extreme helpfulness of error message. yet of time explains problem pretty straightforward. say, if says particular table doesn't exist, have check spelling, typos, letter case, credentials , such. or, if says there error in sql syntax, have examine sql. , problem spot right before query part cited in error messaage.
you have trust error message. if says number of tokens doesn't match number of bound variables is so. same goes absent tables or columns. given choice, whether it's own mistake or error message wrong, stick former. again sounds condescending, hundreds of questions on site prove advise extremely useful.
note in order see pdo errors, have able see php errors in general. so, have configure php depends on site environment:
on development server handy have errors right on screen, displaying errors have turned on:
error_reporting(e_all); ini_set('display_errors',1);
while on live site, errors have logged, never shown client. this, configure php way:
error_reporting(e_all); ini_set('display_errors', 0); ini_set('log_errors', 1);
note error_reporting
should set e_all
time.
also note despite common delusion, no try-catch have used error reporting. php report pdo errors already, , in way better form. uncaught exception development, yet if want show customized error page, still don't use try catch this, set custom error handler. in nutshell, don't have treat pdo errors special regard them other error in code.
Comments
Post a Comment