Skip to content

Commit 8869bbe

Browse files
committed
Fix bug GH-7746 (mysqli_sql_exception->sqlstate is inaccessible)
Closes GH-7747
1 parent 24be11f commit 8869bbe

File tree

5 files changed

+48
-1
lines changed

5 files changed

+48
-1
lines changed

NEWS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ PHP NEWS
3131
. Fixed bug #81658 (MYSQL_OPT_LOAD_DATA_LOCAL_DIR not available in MariaDB).
3232
(devnexen)
3333
. Introduced MYSQLI_IS_MARIADB. (devnexen)
34+
. Fixed bug GH-7746 (mysqli_sql_exception->getSqlState()). (Kamil Tekiela)
3435

3536
- OCI8:
3637
. Fixed bug GH-7765 (php_oci_cleanup_global_handles segfaults at second

ext/mysqli/mysqli.stub.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -613,6 +613,8 @@ public function next(): bool {}
613613
final class mysqli_sql_exception extends RuntimeException
614614
{
615615
protected string $sqlstate = "00000";
616+
617+
public function getSqlState(): string {}
616618
}
617619

618620
/** @refcount 1 */

ext/mysqli/mysqli_arginfo.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* This is a generated file, edit the .stub.php file instead.
2-
* Stub hash: e9f4dd04e7d01864c38033bcaf5e03b63e191deb */
2+
* Stub hash: 78662c05cd463735a8a4101c0357fd0d2698d48e */
33

44
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_mysqli_affected_rows, 0, 1, MAY_BE_LONG|MAY_BE_STRING)
55
ZEND_ARG_OBJ_INFO(0, mysql, mysqli, 0)
@@ -720,6 +720,9 @@ ZEND_END_ARG_INFO()
720720

721721
#define arginfo_class_mysqli_warning_next arginfo_mysqli_thread_safe
722722

723+
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_mysqli_sql_exception_getSqlState, 0, 0, IS_STRING, 0)
724+
ZEND_END_ARG_INFO()
725+
723726

724727
ZEND_FUNCTION(mysqli_affected_rows);
725728
ZEND_FUNCTION(mysqli_autocommit);
@@ -842,6 +845,7 @@ ZEND_METHOD(mysqli_result, getIterator);
842845
ZEND_METHOD(mysqli_stmt, __construct);
843846
ZEND_METHOD(mysqli_warning, __construct);
844847
ZEND_METHOD(mysqli_warning, next);
848+
ZEND_METHOD(mysqli_sql_exception, getSqlState);
845849

846850

847851
static const zend_function_entry ext_functions[] = {
@@ -1083,6 +1087,7 @@ static const zend_function_entry class_mysqli_warning_methods[] = {
10831087

10841088

10851089
static const zend_function_entry class_mysqli_sql_exception_methods[] = {
1090+
ZEND_ME(mysqli_sql_exception, getSqlState, arginfo_class_mysqli_sql_exception_getSqlState, ZEND_ACC_PUBLIC)
10861091
ZEND_FE_END
10871092
};
10881093

ext/mysqli/mysqli_exception.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,17 @@ void php_mysqli_throw_sql_exception(char *sqlstate, int errorno, char *format, .
6363

6464
zend_throw_exception_object(&sql_ex);
6565
}
66+
67+
PHP_METHOD(mysqli_sql_exception, getSqlState)
68+
{
69+
zval *prop;
70+
zval rv;
71+
72+
ZEND_PARSE_PARAMETERS_NONE();
73+
74+
prop = zend_read_property(mysqli_exception_class_entry, Z_OBJ_P(ZEND_THIS), "sqlstate", sizeof("sqlstate")-1, 1, &rv);
75+
ZVAL_DEREF(prop);
76+
zend_string *str = zval_get_string(prop);
77+
78+
RETURN_STR(str);
79+
}

ext/mysqli/tests/gh7746.phpt

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
--TEST--
2+
Bug #GH-7746 mysqli_sql_exception->sqlstate is inaccessible
3+
--EXTENSIONS--
4+
mysqli
5+
--SKIPIF--
6+
<?php
7+
require_once "connect.inc";
8+
require_once 'skipifconnectfailure.inc';
9+
?>
10+
--FILE--
11+
<?php
12+
require 'connect.inc';
13+
$link = my_mysqli_connect($host, $user, $passwd, $db, $port, $socket);
14+
15+
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
16+
17+
try {
18+
$link->query("stuff");
19+
} catch (mysqli_sql_exception $exception) {
20+
var_dump($exception->getSqlState());
21+
}
22+
23+
?>
24+
--EXPECT--
25+
string(5) "42000"

0 commit comments

Comments
 (0)