php - How to Print to Query Code [PDO] -
try { $dbh = new pdo("mysql:host=$host", $root, $root_password); $dbh->exec("insert test (col1, col2, col3) values (test1, test1, test1)") or die(print_r($dbh->errorinfo(), true)); } catch (pdoexception $e) { die("db error: ". $e->getmessage()); }
i used print on screen sql query: $sth->debugdumpparams();
internal 500 error
how can print query?
almost every line of code snippet wrong. there no $sth
debugdumpparams starter. not mention way running query totally wrong.
here how should be
$dbh = new pdo("mysql:host=$host", $root, $root_password); $dbh->setattribute( pdo::attr_errmode, pdo::errmode_exception ); $sql = "insert test (col1, col2, col3) values (?,?,?)"; $sth = $dbh->prepare($sql); $sth->execute(['test1', 'test1', 'test1'])
now can use whatever method echo query, debugdumpparams or echo $sql, neither of because print same have in script already:
insert test (col1, col2, col3) values (?,?,?)
yet suppose need not query error message, code above supply - run it.
note should avoid either die()
or try
statements in code.
Comments
Post a Comment