I'm fetching data in Arabic from MySQL tables with MySQLi. So I usually use this in procedural style:
mysql_query["SET NAMES 'utf8'"];
mysql_query['SET CHARACTER SET utf8'];
Now I am using the OOP style so I am trying to see if there is something I could set rather than the above?
I only found this in PHP manual so I did it, but what about setting names to UTF8?
$mysqli->set_charset["utf8"];
Mark Amery
131k78 gold badges392 silver badges441 bronze badges
asked May 31, 2012 at 8:19
2
It's the same:
$mysqli->query["SET NAMES 'utf8'"];
From the manual:
This is the preferred way to change the charset. Using mysqli::query[] to execute SET NAMES .. is not recommended.
$mysqli->set_charset["utf8"];
is just enough, let the mysqli db driver do the thing for you.
answered May 31, 2012 at 8:24
xdazzxdazz
156k36 gold badges240 silver badges268 bronze badges
4
You should use
$mysqli->set_charset["utf8"];
Don't use SET NAMES
or SET CHARACTER SET
explicitly when using MySQLi., and certainly don't use both like the question asker here originally was. Reasons that this is a bad idea:
- Calling
SET CHARACTER SET utf8
afterSET NAMES utf8
actually just undoes some of the work thatSET NAMES
did. The PHP manual explicitly warns us to use
mysqli_set_charset
, notSET NAMES
:This is the preferred way to change the charset. Using mysqli_query[] to set it [such as SET NAMES utf8] is not recommended. See the MySQL character set concepts section for more information.
Under the hood,
mysqli_set_charset
is just a wrapper formysql_set_character_set
from the MySQL C API [or its mysqlnd equivalent]. Looking at the docs for that function, we can see the difference between it andSET NAMES
:This function works like the
SET NAMES
statement, but also sets the value ofmysql->charset
, and thus affects the character set used bymysql_real_escape_string[]
In other words,
$mysqli->set_charset['foo']
will do everythingSET NAMES foo
does and also ensure thatmysqli_real_escape_string
respects the new encoding. Admittedly, if you're only using encodings like Latin 1 and UTF 8 that strictly extend ASCII [that is, which encode all ASCII strings exactly as they would be encoded in ASCII], then usingSET NAMES
instead ofset_charset
won't break anything. However, if you're using more unusual encodings like GBK, then you could end up garbling your strings or even introducing SQL injection vulnerabilities that bypassmysqli_real_escape_string
.
It's thus good practice to only use set_charset
, not SET NAMES
. There's nothing that SET NAMES
does that set_charset
doesn't, and set_charset
avoids the risk of mysqli_real_escape_string
behaving incorrectly [or even insecurely].
answered May 2, 2016 at 13:12
Mark AmeryMark Amery
131k78 gold badges392 silver badges441 bronze badges
You can use this. Its realy nice for mysqli Character Set
$dbhost = "localhost";
$dbuser = "root";
$dbpass = "dbpass";
$dbname = "dbname";
$conn = mysqli_connect[$dbhost,$dbuser,$dbpass,$dbname];
mysqli_query[$conn,"SET CHARACTER SET 'utf8'"];
mysqli_query[$conn,"SET SESSION collation_connection ='utf8_unicode_ci'"];
answered Oct 24, 2017 at 15:39
0
For procedural style lovers.
// Create connection
$conn = mysqli_connect[$servername, $username, $password, $dbname];
/* change character set to utf8 */
mysqli_set_charset[$conn,"utf8"];
answered Nov 21, 2017 at 9:56
1
mysqli_query[$conn,"SET NAMES 'latin5'"];
$conn = connection string
answered May 2, 2016 at 10:43
alpcalpc
5383 silver badges6 bronze badges
1