Bug #72748 [Com]: MySQLi error variables gets cleared executing any line of code after query

From: Date: Wed, 03 Aug 2016 17:35:30 +0000
Subject: Bug #72748 [Com]: MySQLi error variables gets cleared executing any line of code after query
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-202892@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=72748&edit=1

 ID:                 72748
 Comment by:         mdutra at mdisys dot net
 Reported by:        mdutra at mdisys dot net
 Summary:            MySQLi error variables gets cleared executing any
                     line of code after query
 Status:             Open
 Type:               Bug
 Package:            MySQLi related
 Operating System:   CENTOS 6.8 x86_64
 PHP Version:        7.0.9
 Block user comment: N
 Private report:     N

 New Comment:

To test issue just call the query passing a statement with SQL syntax error.

$db->query("DROP TABLE IF EXISTS xyz oc_rwd_category;");


Previous Comments:
------------------------------------------------------------------------
[2016-08-03 17:26:34] 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();
	}
}



------------------------------------------------------------------------



--
Edit this bug report at https://bugs.php.net/bug.php?id=72748&edit=1


Thread (4 messages)

« previous php.bugs (#202892) next »