php.net |  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
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: gutzmer at usa dot net
New email:
PHP Version: OS:

 

 [2010-07-06 19:07 UTC] gutzmer at usa dot net
Description:
------------
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.


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-07-06 19:29 UTC] andrey@php.net
-Status: Open +Status: Verified -Assigned To: +Assigned To: mysql
 [2010-08-26 11:26 UTC] uw@php.net
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:
http://dev.mysql.com/doc/refman/5.1/en/mysql-set-character-set.html

So, use mysqli_set_charset() to set the charset and then SET NAMES to change the collation.
 [2010-09-27 10:30 UTC] uw@php.net
-Status: Verified +Status: Bogus
 [2010-09-27 10:30 UTC] uw@php.net
See last comment - not needed.
 
PHP Copyright © 2001-2021 The PHP Group
All rights reserved.
Last updated: Thu Dec 02 08:03:36 2021 UTC