MySQL/PHP5-获取最后获取的行的ID

MySQL/PHP5-获取最后获取的行的ID,php,mysql,Php,Mysql,我正在尝试根据mysql_fetch_assoc返回的结果创建一个查找表,这是我到目前为止的代码: protected function dumpResultAsHash($primaryKey) { if (empty($primaryKey) || !isset($primaryKey)) throw new Exception("Primary key cannot be null or not defined!"); $resultset = array

我正在尝试根据mysql_fetch_assoc返回的结果创建一个查找表,这是我到目前为止的代码:

protected function dumpResultAsHash($primaryKey)
{
    if (empty($primaryKey) || !isset($primaryKey))
        throw new Exception("Primary key cannot be null or not defined!");

    $resultset = array();

    while ($result = mysql_fetch_assoc($this->m_result))
        $resultset[$result[$primaryKey]] =$result;

    return $resultset;
}
但是,我不喜欢必须指定主键的列名并将其提供给函数这一事实。是否有一种方法可以确定每条记录的主键值,比如mysql\u insert\u id,但对于我的情况,比如最后获取的id?

根据,您可以通过以下方式获取表的主键:

SELECT k.column_name
FROM information_schema.table_constraints t
JOIN information_schema.key_column_usage k
USING(constraint_name,table_schema,table_name)
WHERE t.constraint_type='PRIMARY KEY'
  AND t.table_schema='db'
  AND t.table_name='tbl';
但是,老实说,我只需要知道主键就可以通过它进行索引。例如,主键可以是多部分键。这并不像你想象的那么简单