php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #72748 MySQLi error variables gets cleared executing any line of code after query
Submitted: 2016-08-03 17:26 UTC Modified: 2017-05-21 04:22 UTC
Votes:3
Avg. Score:4.3 ± 0.9
Reproduced:2 of 3 (66.7%)
Same Version:1 (50.0%)
Same OS:1 (50.0%)
From: mdutra at mdisys dot net Assigned:
Status: No Feedback Package: MySQLi related
PHP Version: 7.0.9 OS: CENTOS 6.8 x86_64
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [2016-08-03 17:26 UTC] mdutra at mdisys dot net
Description:
------------
---
From manual page: http://www.php.net/mysqli.error
---

I have consistently reproduced this issue in WAMP development and LAMP PHP7 production environments. When mysqli executes a query with sql syntax error it returns the errno, error and error_list values; but immediately after executing another line of code (any code at all) these values are cleared making it impossible to return the error number and description. I've tried several alternatives to retrieve these error values to no use. It is most likely a PHP 7 MySQLi driver issue. 

Test script:
---------------
<?php
namespace DB;
final class MySQLi {
	private $connection;

	public function __construct($hostname, $username, $password, $database, $port = '3306') {
		$this->connection = new \mysqli($hostname, $username, $password, $database, $port);

		if ($this->connection->connect_error) {
			throw new \Exception('Error: ' . mysql_error($this->connection) . '<br />Error No: ' . mysql_errno($this->connection) . '<br /> Error in: <b>' . $trace[1]['file'] . '</b> line <b>' . $trace[1]['line'] . '</b><br />' . $sql);
		}

		$this->connection->set_charset("utf8");
		$this->connection->query("SET SQL_MODE = ''");
	}

	public function query($sql) {
		$query = $this->connection->query($sql);

                //*** Test line to prove any line of code executed does wipe off the error variables values.
                $errmsg = $this->connection->error;

		if ($query) {
			if ($query instanceof \mysqli_result) {
				$data = array();

				while ($row = $query->fetch_assoc()) {
					$data[] = $row;
				}

				$result = new \stdClass();
				$result->num_rows = $query->num_rows;
				$result->row = isset($data[0]) ? $data[0] : array();
				$result->rows = $data;

				$query->close();

				return $result;
			} else {
				return true;
			}
		} else {
			throw new \Exception('Error: ' . $this->connection->error  . '<br />Error No: ' . $this->connection->errno . '<br />' . $sql);
		}
	}

	public function escape($value) {
		return $this->connection->real_escape_string($value);
	}
	
	public function countAffected() {
		return $this->connection->affected_rows;
	}

	public function getLastId() {
		return $this->connection->insert_id;
	}
	
	public function connected() {
		return $this->connection->connected();
	}
	
	public function __destruct() {
		$this->connection->close();
	}
}


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2016-08-03 17:35 UTC] mdutra at mdisys dot net
To test issue just call the query passing a statement with SQL syntax error.

$db->query("DROP TABLE IF EXISTS xyz `oc_rwd_category`;");
 [2017-05-10 13:39 UTC] fjanisze@php.net
-Status: Open +Status: Feedback
 [2017-05-10 13:39 UTC] fjanisze@php.net
Hi, can I ask you to attempt to reproduce this issue with the latest environment releases? We do have the suspect that this issue shall not appear anymore, indeed I'm not able to reproduce it locally.
 [2017-05-21 04:22 UTC] php-bugs at lists dot php dot net
No feedback was provided. The bug is being suspended because
we assume that you are no longer experiencing the problem.
If this is not the case and you are able to provide the
information that was requested earlier, please do so and
change the status of the bug back to "Re-Opened". Thank you.
 
PHP Copyright © 2001-2022 The PHP Group
All rights reserved.
Last updated: Mon Sep 26 12:05:52 2022 UTC