[英]MySQL/PHP 5 - getting the ID of the last fetched row
我正在尝试从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_insert_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';
但是,老实说,我只需要知道主键才能对其进行索引。 例如,主键可以是多部分键。 这并不像您想的那么简单。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.