0
我想從mysql INSERT查詢中返回插入的id。每次運行函數時,我都會得到0。有人可以請解釋我可以檢索的價值點,因爲雖然下面的腳本執行,我不能退出插入的ID。可能做了一些愚蠢的事情。insert_id mysqli
<?php
public function execSQL($sql, $params, $close){
$mysqli = new mysqli(DB_HOST,DB_USER,DB_PASSWORD,DB_NAME);
$stmt = $mysqli->prepare($sql) or die ("Failed to prepared the statement!");
call_user_func_array(array($stmt, 'bind_param'), $this->refValues($params));
$this->insert_id($this->connection);
$stmt->execute();
if($close){
$result = $mysqli->affected_rows;
} else {
$meta = $stmt->result_metadata();
while ($field = $meta->fetch_field()) {
$parameters[] = &$row[$field->name];
}
call_user_func_array(array($stmt, 'bind_result'), $this->refValues($parameters));
while ($stmt->fetch()) {
$x = array();
foreach($row as $key => $val) {
$x[$key] = $val;
}
$results[] = $x;
}
$result = $results;
}
$stmt->close();
$mysqli->close();
return $result;
}
?>
感謝您的幫助。我設法抓住最後一個insert_id,現在我遇到的問題是,因爲我上傳了超過1張圖片,所以返回了幾個insert_id。有沒有一種方法可以使用這些值之一來識別數據庫中的所有三個文件/圖像? – paynod 2011-03-31 20:16:53
我不確定我是否理解你的問題。例如,如果插入3條記錄,則將數組中插入的ID存儲。稍後,您可以'SELECT ... WHERE id IN(id1,id2,id3)'(您可以通過'implode(「,」,$ array_of_ids)'將數組中的ids列表轉換爲字符串'; – a1ex07 2011-03-31 20:27:09