Mysqli_insert_id still returns a value even after selecting queries

I have a function that does not work as I expect. Its purpose is to return an array of strings for selected queries or an insert identifier for Insert queries. For some reason, if there is an insert request followed by a select request, part of the function is where it checks if the request was an insert or not, because inesrt_id still returns the id of the inserted row. I could program around this, but I really would like to understand why this is happening.

My understanding was that if the most recent request is a choice, then there should be no insert identifier. I'm new to mysqli, so maybe the query doesn't really end when I think it makes the new select be considered part of the same query? I can make it work fine if I recreate the connection on every request, but this is impractical. Here is the code for the request function.

public function __construct(){
    parent::__construct();
    //        @mysql_select_db ('public') OR die('Could not select the database: ' . mysql_error() );
    $this->connection = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_TABLE, DB_PORT)OR die ('Could not connect to MySQL: ' . mysql_error());
    $this->db = $this;
}

public function query_return($querystring){

    $response = array();
    $this->result_object = $this->connection->query($querystring);
    if($this->result_object != FALSE) {
        $last_id = $this->connection->insert_id;
        if (!empty($last_id)) {
            $response = $last_id;
        }
        else {
            while ($row = mysqli_fetch_assoc($this->result_object)) {
                array_push($response, $row);
            }
        }
    }
    else{
        $response = PEAR::raiseError('There was a problem with the query: '. mysql_error());
    }
    return $response;


}

EDIT: , is_object . / , , , . insert_id is_object, . , insert_id , , , , , FALSE query_return.

, mysqli_insert_id , , , , , . query_return "mysql_" ( "i" ) , , - . .

private function query_return($querystring){

    $response = array();
    $this->result_object = $this->connection->query($querystring);
    if($this->result_object != FALSE) {
        if (!is_object($this->result_object)) {
            $last_id = $this->connection->insert_id;
            if (!empty($last_id)) {
                $response = $last_id;
            }
        }
        else {
            while ($row = mysqli_fetch_assoc($this->result_object)) {
                array_push($response, $row);
            }
        }
    }
    else{
        $response = PEAR::raiseError('There was a problem with the query: '. mysql_error());
    }
    return $response;
}
+4
3

PHP, https://dev.mysql.com/doc/refman/5.0/en/mysql-stmt-insert-id.html

,

LAST_INSERT_ID() ( ) BIGINT (64-), , AUTO_INCREMENT INSERT

https://dev.mysql.com/doc/refman/5.0/en/information-functions.html#function_last-insert-id

if

 if (!is_object($this->result_object)) {

, -

PHP, , , , ,

+2

, . unset ($ last_id); . , . .

public function __construct(){
    parent::__construct();
    //        @mysql_select_db ('public') OR die('Could not select the database: ' . mysql_error() );
    $this->connection = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_TABLE, DB_PORT)OR die ('Could not connect to MySQL: ' . mysql_error());
    $this->db = $this;
}

public function query_return($querystring){

    $response = array();
    $this->result_object = $this->connection->query($querystring);
    if($this->result_object != FALSE) {
        $last_id = $this->connection->insert_id;
        if (!empty($last_id)) {
            $response = $last_id;
        }
        else {
            while ($row = mysqli_fetch_assoc($this->result_object)) {
                array_push($response, $row);
            }
        }
        unset($last_id);    //<--------here
    }
    else{
        $response = PEAR::raiseError('There was a problem with the query: '. mysql_error());
    }
    return $response;


}
0

, mysqli_insert_id , . , 0.

0
source

All Articles