2011-03-31 29 views
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; 
} 
?> 

回答

0

执行插入查询后检查$mysqli->insert_id

+0

感谢您的帮助。我设法抓住最后一个insert_id,现在我遇到的问题是,因为我上传了超过1张图片,所以返回了几个insert_id。有没有一种方法可以使用这些值之一来识别数据库中的所有三个文件/图像? – paynod 2011-03-31 20:16:53

+0

我不确定我是否理解你的问题。例如,如果插入3条记录,则将数组中插入的ID存储。稍后,您可以'SELECT ... WHERE id IN(id1,id2,id3)'(您可以通过'implode(“,”,$ array_of_ids)'将数组中的ids列表转换为字符串'; – a1ex07 2011-03-31 20:27:09

相关问题