2010-08-09 21 views
0

我試圖創建其結果mysql_fetch_assoc返回查找表的ID,這是我的代碼至今:的MySQL/PHP 5 - 獲取最後讀取行

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?

回答

1

this,你可以得到一個表通過主鍵:

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'; 

但是,說實話,我只是需要知道,以指數它的主鍵去。例如,主鍵可以是多部分鍵。這並不像你想象的那麼簡單。