insert_id mysqli

时间:2011-03-31 19:37:46

标签: mysqli insert-id

我正在尝试从mysql INSERT查询中返回插入的id。每次运行该函数时,我得到0作为结果。有人可以解释我可以在什么时候检索值,因为虽然下面的脚本执行我不能退出插入的ID。可能做了一些愚蠢的事。

<?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;
}
?>

1 个答案:

答案 0 :(得分:0)

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