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

From: Date: Wed, 03 Aug 2016 17:26:38 +0000
Subject: Bug #72748 [NEW]: MySQLi error variables gets cleared executing any line of code after query
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-202891@lists.php.net to get a copy of this message
From:             mdutra at mdisys dot net
Operating system: CENTOS 6.8 x86_64
PHP version:      7.0.9
Package:          MySQLi related
Bug Type:         Bug
Bug description:MySQLi error variables gets cleared executing any line of code after query

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 bug report at https://bugs.php.net/bug.php?id=72748&edit=1
-- 
Try a snapshot (PHP 5.4):   https://bugs.php.net/fix.php?id=72748&r=trysnapshot54
Try a snapshot (PHP 5.5):   https://bugs.php.net/fix.php?id=72748&r=trysnapshot55
Try a snapshot (trunk):     https://bugs.php.net/fix.php?id=72748&r=trysnapshottrunk
Fixed in SVN:               https://bugs.php.net/fix.php?id=72748&r=fixed
Fixed in release:           https://bugs.php.net/fix.php?id=72748&r=alreadyfixed
Need backtrace:             https://bugs.php.net/fix.php?id=72748&r=needtrace
Need Reproduce Script:      https://bugs.php.net/fix.php?id=72748&r=needscript
Try newer version:          https://bugs.php.net/fix.php?id=72748&r=oldversion
Not developer issue:        https://bugs.php.net/fix.php?id=72748&r=support
Expected behavior:          https://bugs.php.net/fix.php?id=72748&r=notwrong
Not enough info:            https://bugs.php.net/fix.php?id=72748&r=notenoughinfo
Submitted twice:            https://bugs.php.net/fix.php?id=72748&r=submittedtwice
register_globals:           https://bugs.php.net/fix.php?id=72748&r=globals
PHP 4 support discontinued: https://bugs.php.net/fix.php?id=72748&r=php4
Daylight Savings:           https://bugs.php.net/fix.php?id=72748&r=dst
IIS Stability:              https://bugs.php.net/fix.php?id=72748&r=isapi
Install GNU Sed:            https://bugs.php.net/fix.php?id=72748&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=72748&r=float
No Zend Extensions:         https://bugs.php.net/fix.php?id=72748&r=nozend
MySQL Configuration Error:  https://bugs.php.net/fix.php?id=72748&r=mysqlcfg



Thread (4 messages)

« previous php.bugs (#202891) next »