Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set character set using MySQLi

Tags:

php

mysqli

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");
like image 787
sys_debug Avatar asked May 31 '12 08:05

sys_debug


People also ask

What is character set in MySQL?

A character set in MySQL is a set of characters, encodings, and symbols that are legal in a string. This article explains how we can get all character sets in MySQL, how we can configure proper character sets for client connections, and how we can convert strings between multiple character sets.

What is Mysqli_query () used for?

Definition and Usage. The query() / mysqli_query() function performs a query against a database.

Is MySQLi faster than MySQL?

The MySQL extension is very slightly faster than MySQLi in most benchmarks I've seen reported. The difference is so slight, however, that this should probably not be your criterion for deciding between the two. Other factors dwarf the difference in performance between mysql and mysqli.

How configure MySQLi in PHP?

Open your php. ini file ( php configuration file ) inside your PHP directory ( or windows directory ) . Search for mysqli and enable the dll by removing ; before it. You may have to re-boot your system.


2 Answers

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.

like image 65
xdazz Avatar answered Sep 21 '22 04:09

xdazz


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 after SET NAMES utf8 actually just undoes some of the work that SET NAMES did.
  • The PHP manual explicitly warns us to use mysqli_set_charset, not SET 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 for mysql_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 and SET NAMES:

    This function works like the SET NAMES statement, but also sets the value of mysql->charset, and thus affects the character set used by mysql_real_escape_string()

    In other words, $mysqli->set_charset('foo') will do everything SET NAMES foo does and also ensure that mysqli_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 using SET NAMES instead of set_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 bypass mysqli_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).

like image 36
Mark Amery Avatar answered Sep 23 '22 04:09

Mark Amery