PDO lastInsertID Fatal Error -
i have class named db. when insert in table, returning error message:
call member function lastinsertid() on null in /home/...
everything ok why problem occurs don't have idea. codes:
class db { public static $pdo = null; public static $last_stmt = null; public static function instance() { return ($pdo == null ? self::init() : $pdo); } public static function init() { $pdo = new pdo("mysql:host=" . mysql_host . ";dbname=" . mysql_db, mysql_user, mysql_pass); $pdo->exec("set names utf8"); $pdo->setattribute(pdo::attr_default_fetch_mode, pdo::fetch_obj); return $pdo; } public static function query($query, $bindings = null) { if( is_null($bindings) ) { if( !($last_stmt = self::instance()->query($query)) ) { return false; } } else { $last_stmt = self::prepare($query); if( !$last_stmt->execute($bindings) ) { return false; } } return $last_stmt; } public static function insert($query, $bindings = null) { if( !($stmt = self::query($query, $bindings)) ) { return false; } return $pdo->lastinsertid(); }
}
Comments
Post a Comment