开发者

insert_id mysqli

I'm trying to return the inserted id from a mysql INSERT query. Each time I run the function I get 0 as the result. Can someone please explain at what point I can retrieve the value because although the script below executes I cannot retireve the inserted id. Probably done something stupid.

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


Check $mysqli->insert_id after executing insert query.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜