在mysql中返回最后一个插入id是得到0


Returning the last insert id in mysql is getting 0?

我使用以下代码从表员工返回最后插入id,请让我知道以下代码有什么问题:

<?php 
$temp_array = mysql_query("select last_insert_id() from employee"); 
//now you can display it, to test it 
echo $temp_array;
?>

它返回0值。请告诉我不使用max()的简单方法,

mysql_insert_id可用于返回前一个查询为自动递增列生成的最后一个ID(如果该列是自动递增的)

$temp_array的类型是Resource,而不是integer。你需要做

$rslt = mysql_fetch_array($temp_array);
$id = $rslt[0];

详情见http://php.net/manual/en/function.mysql-fetch-array.php