Skip to content

Commit 74d1699

Browse files
kamil-tekielanikic
authored andcommitted
mysqli_set_charset now throws an mysqli_sql_exception when incorrect charset is provided
Closes GH-6142.
1 parent 6454766 commit 74d1699

File tree

3 files changed

+20
-6
lines changed

3 files changed

+20
-6
lines changed

ext/mysqli/mysqli_nonapi.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1040,6 +1040,7 @@ PHP_FUNCTION(mysqli_set_charset)
10401040
MYSQLI_FETCH_RESOURCE_CONN(mysql, mysql_link, MYSQLI_STATUS_VALID);
10411041

10421042
if (mysql_set_character_set(mysql->mysql, cs_name)) {
1043+
MYSQLI_REPORT_MYSQL_ERROR(mysql->mysql);
10431044
RETURN_FALSE;
10441045
}
10451046
RETURN_TRUE;

ext/mysqli/tests/mysqli_set_charset.phpt

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,20 @@ if ((($res = mysqli_query($link, 'SHOW CHARACTER SET LIKE "latin1"', MYSQLI_STOR
101101
}
102102
mysqli_free_result($res);
103103

104+
// Make sure that set_charset throws an exception in exception mode
105+
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
106+
try {
107+
$link->set_charset('invalid');
108+
} catch (\mysqli_sql_exception $exception) {
109+
echo "Exception: " . $exception->getMessage() . "\n";
110+
}
111+
112+
try {
113+
$link->set_charset('ucs2');
114+
} catch (\mysqli_sql_exception $exception) {
115+
echo "Exception: " . $exception->getMessage() . "\n";
116+
}
117+
104118
mysqli_close($link);
105119

106120
try {
@@ -115,6 +129,8 @@ if ((($res = mysqli_query($link, 'SHOW CHARACTER SET LIKE "latin1"', MYSQLI_STOR
115129
<?php
116130
require_once("clean_table.inc");
117131
?>
118-
--EXPECT--
132+
--EXPECTF--
133+
Exception: %s
134+
Exception: Variable 'character_set_client' can't be set to the value of 'ucs2'
119135
mysqli object is already closed
120136
done!

ext/mysqlnd/mysqlnd_connection.c

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1152,18 +1152,15 @@ MYSQLND_METHOD(mysqlnd_conn_data, set_charset)(MYSQLND_CONN_DATA * const conn, c
11521152
DBG_INF_FMT("conn=%llu cs=%s", conn->thread_id, csname);
11531153

11541154
if (!charset) {
1155-
SET_CLIENT_ERROR(conn->error_info, CR_CANT_FIND_CHARSET, UNKNOWN_SQLSTATE,
1156-
"Invalid characterset or character set not supported");
1155+
SET_CLIENT_ERROR(conn->error_info, CR_CANT_FIND_CHARSET, UNKNOWN_SQLSTATE, "Invalid character set was provided");
11571156
DBG_RETURN(ret);
11581157
}
11591158

11601159
if (PASS == conn->m->local_tx_start(conn, this_func)) {
11611160
char * query;
11621161
size_t query_len = mnd_sprintf(&query, 0, "SET NAMES %s", csname);
11631162

1164-
if (FAIL == (ret = conn->m->query(conn, query, query_len))) {
1165-
php_error_docref(NULL, E_WARNING, "Error executing query");
1166-
} else if (conn->error_info->error_no) {
1163+
if (FAIL == (ret = conn->m->query(conn, query, query_len)) || conn->error_info->error_no) {
11671164
ret = FAIL;
11681165
} else {
11691166
conn->charset = charset;

0 commit comments

Comments
 (0)