|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #52267 add collation to mysqli::set_charset
Submitted: 2010-07-06 19:07 UTC Modified: 2010-09-27 10:30 UTC
From: gutzmer at usa dot net Assigned: mysql (profile)
Status: Not a bug Package: MySQLi related
PHP Version: Irrelevant OS:
Private report: No CVE-ID: None
 [2010-07-06 19:07 UTC] gutzmer at usa dot net
According to the docs,  mysqli::set_charset "is the preferred way to change the charset. Using mysqli::query() to execute SET NAMES ..  is not recommended."

But when using the MySQLi extension, the only way to use a collation other than the default for a particular charset is to send a SET NAMES ... with a COLLATION clause.

mysqli::set_charset should have a second (Optional) "string $collation" parameter to allow users to set the charset and collation without resorting to the 'not recommended' direct query method.  When the optional parameter is not set, is null, casts to Boolean FALSE, or is an invalid choice for the charset passed in the first parameter, the connection should use the default collation for the selected charset.

MySQL already handles error conditions relating to charset/collation mismatches, so there should be no great added overhead to the source code logic implementing  the MySQLi function.


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2010-07-06 19:29 UTC]
-Status: Open +Status: Verified -Assigned To: +Assigned To: mysql
 [2010-08-26 11:26 UTC]
Such a function is not needed and does not exist in MySQL C API. 

You shall not use SET NAMES to change a *charset* because an existing connection handle will not be aware of the change. The connection handle will continue to use the charset set during the creation of the connection. The connection handle will potentially use the wrong charset for mysqli_real_escape_string(). 

This is why you shall use mysqli_set_character_set to set the *charset* because this is something the connection handle can detect for use with mysqli_real_escape_string().

The *collation* is about sort order. Collation is irrelevant for escaping strings on the client. And this is why the C API mysql_set_character_set does not have the additional parameter you want:

So, use mysqli_set_charset() to set the charset and then SET NAMES to change the collation.
 [2010-09-27 10:30 UTC]
-Status: Verified +Status: Bogus
 [2010-09-27 10:30 UTC]
See last comment - not needed.
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Apr 25 12:01:31 2024 UTC