this is table schema:
CREATE TABLE `USERS` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`email` varchar(30) DEFAULT '',
`first_name` varchar(15) DEFAULT '',
`last_name` varchar(15) DEFAULT '',
`password` varchar(15) DEFAULT '',
`gender` int(1) DEFAULT '-1',
PRIMARY KEY (`id`)
)
in php:
$sql = 'INSERT INTO Users (email, first_Name, last_Name, password, gender ) VALUES ("'.$email.'", "'.$first_name.'", "'.$last_name.'", "'.$password.'", '.$gender.')';
try {
$db = getConnection();
$stmt = $db->prepare($sql);
$stmt->execute();
//$user = $stmt->fetchObject();
echo 'last id was '.mysql_insert_id();
$db = null;
} catch(PDOException $e) {
echo '{"error":{"text":'. $e->getMessage() .'}}';
}
I can’t figure out why mysql_insert_id() returns 0. there are no other processes running. id is set to auto increment. Insert is done and seen in db.
You are using
PDOto interface with the database. UsePDO::lastInsertId()to get the last inserted id.mysql_insert_id()is part of theext/mysqlextension. You cannot mix-match functions from both extensions to interface with the same connection.