0
這樣做是表模式:mysql_insert_id()0,但插入成功
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`)
)
在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() .'}}';
}
我想不通爲什麼mysql_insert_id()返回0有沒有其他進程在運行。 id設置爲自動增量。插入完成並在db中看到。
does'$ stmt-> execute()'do'mysql_query'? –
您正在將PDO與舊的'mysql_'驅動程序混合,這就是問題所在 – bfavaretto