Php mysqli_insert_id即使在select查询之后仍然返回值

Php mysqli_insert_id即使在select查询之后仍然返回值,php,mysql,sql,Php,Mysql,Sql,我有一个函数并没有按我期望的那样工作。其目的是为select查询返回行数组,或为insert查询返回insert id。出于某种原因,如果有一个insert查询后跟一个select查询,则函数中测试该查询是否为insert的部分将失败,因为inesrt_id仍然返回插入行的id。我可以围绕这个进行编程,但我真的很想理解为什么会发生这种情况 我的理解是,如果最近的查询是select,那么就不应该有insert id。我是mysqli的新手,所以当我认为查询真的“结束”时,可能查询并没有真正“结束”

我有一个函数并没有按我期望的那样工作。其目的是为select查询返回行数组,或为insert查询返回insert id。出于某种原因,如果有一个insert查询后跟一个select查询,则函数中测试该查询是否为insert的部分将失败,因为inesrt_id仍然返回插入行的id。我可以围绕这个进行编程,但我真的很想理解为什么会发生这种情况

我的理解是,如果最近的查询是select,那么就不应该有insert id。我是mysqli的新手,所以当我认为查询真的“结束”时,可能查询并没有真正“结束”,所以新的select将作为同一查询的一部分计算?如果我在每个查询上重新创建连接,我可以让它正常工作,但这并不实际。下面是查询函数的代码

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;


}
编辑:好的,多亏了下面的人,我在函数中添加了一个is\u对象测试。我不能直接替换真/假测试,因为我实际上也使用这个函数进行删除,我想知道它什么时候失败。我用is_对象测试包装了insert_id测试,现在似乎一切正常。我猜想,如果连接上有前一个insert,那么删除查询将返回insert\u id,但我认为这不会导致问题,因为这些函数只有在从查询返回中收到FALSE时才会失败

我仍然不明白为什么mysqli_insert_id会在select查询后返回一个值,而文档让我相信它不应该返回值,但至少我的代码在工作。这个query_返回函数是从这些方法的'mysql_'(no'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;
}

我相信变量保留了上次调用时的值。尝试添加一个未设置的($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;


}
public function\uuuu construct(){
父项::_构造();
//@mysql_select_db('public')或die('无法选择数据库:'。mysql_error());
$this->connection=new mysqli(DB_主机、DB_用户、DB_PASS、DB_表、DB_端口)或die('无法连接到MySQL:'。MySQL_错误());
$this->db=$this;
}
公共函数查询返回($querystring){
$response=array();
$this->result\u object=$this->connection->query($querystring);
if($this->result\u object!=FALSE){
$last\u id=$this->connection->insert\u id;
如果(!空($last_id)){
$response=$last_id;
}
否则{
而($row=mysqli\u fetch\u assoc($this->result\u object)){
数组_push($response,$row);
}
}

unset($last_id);//这就是它的工作原理,
mysqli_insert_id
仍然返回一个值,因为它是同一个连接。您需要关闭并打开一个新的连接,才能将其设置为0。

的PHP的

这份文件说

LAST_INSERT_ID()(不带参数)返回一个BIGINT(64位)值,该值表示第一个自动生成的值 由最近执行的INSERT为自动增量列设置 影响此列的语句

我建议将您的if声明更改为

 if (!is_object($this->result_object)) {
这将检查是否有任何要检索的结果


PHP文档可能应该更新以反映这一点,因为它的明显行为

是一个局部变量,它的值将在函数末尾被遗忘“如果最后一个查询不是INSERT或UPDATE语句,或者如果修改后的表没有具有AUTO_INCREMENT属性的列,此函数将从文档返回零”,可能应该更新文档?
mysqli_INSERT_id
几乎肯定不使用
last_INSERT_id()
获取其值。作为wire协议的一部分,服务器在插入后发送未经请求的值。您插入的列是否具有自动增量属性?它不是直接包装器,它经过旧的mysql\u insert\u id,该函数的文档似乎是正确的:并警告该值未重置。在文档中报告了一个bug,这是一个很好的发现。我不这么认为?该函数已经从当前的主分支中删除了,在链接的1568行?这不是一个PHP函数调用,PHP函数已经删除。作为参考,报告了bug,但旧函数确实记录了这种行为。MySQLi应该报告吧