我需要检索自动增量值并在执行插入查询后将其存储在会话中。
我的表名是4个字段ID(主键和自动增量),名称,电话,电子邮件。
$query = mysql_query($con, "insert into out(name, phone, email) values ('$name', '$phone', '$email')");
$id = mysql_insert_id($con);
while($rowval = mysql_fetch_array($query))
{
$id= $rowval['id'];
}
if($query)
{
$_SESSION['id'] = $id;
}
答案 0 :(得分:2)
try this.
$sql = mysql_query($con, "INSERT INTO(col1, col2, col3) values (val1, val2, val3)";
$mysql->query($sql);
$last_inserted_id=$mysql->insert_id; // returns last ID
//you can get last inserted id then set it in session.
答案 1 :(得分:1)
获取最后一个插入ID的最通用方法是简单地选择它。
$result = mysql_query("SELECT LAST_INSERT_ID()");
$lastId = mysql_result($result, 0, 0);
答案 2 :(得分:-1)
$query = mysql_query($con, "insert into out(name, phone, email) values ('$name', '$phone', '$email')");
$id = mysql_insert_id();
//只需删除$ con ...
while($rowval = mysql_fetch_array($query))
{
$id= $rowval['id'];
}
if($query)
{
$_SESSION['id'] = $id;
}