php - want to see SQL error in report -
i have query
insert table (id) values (5);
the table has record such id. query fails.
my mysqli class extension looks this:
<?php class my_mysqli extends mysqli { function __construct($config) { $this->db = parent::__construct($config['host'], $config['user'], $config['pass'], $config['db']); } function exe($sql) { if ( ! $st = $this->db->prepare($sql)) { trigger_error($st->error); // 1 isn't triggered } if ( ! $st->execute()) { trigger_error($st->error); // 1 triggered } // ..then parse results , close } }
right after $mysqli->execute()
log $mysqli->error
, get:
*unknown prepared statement handler (0) given mysqld_stmt_execute*
but see sql error instead:
duplicate entry '5' key 'primary'
there not sense in first block actually. you're doing:
if ( ! $st = $this->db->prepare($sql)) { trigger_error($st->error); // 1 isn't triggered }
"if there no $st object - call object's method".
next 1 better anyway - there no error method or property in mysqli_stmt class.
function exe($sql) { if ( ! $st = $this->db->prepare($sql)) { throw new exception($this->db->error); } if ( ! $st->execute()) { throw new exception($this->db->error); } }
exceptions better can caught , contain stack trace out of box.
by way, there no point in using prepare() without parameters. so, code have
function exe($sql) { if ( ! $this->db->query($sql) ) { throw new exception($this->db->error); } }
Comments
Post a Comment