Search code examples
phpmysqli

Set character set using MySQLi


I'm fetching data in Arabic from MySQL tables with MySQLi. So I usually use this in procedural style:

mysql_query("SET NAMES 'utf8mb4'"); 
mysql_query('SET CHARACTER SET utf8mb4'); 

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("utf8mb4");

Solution

  • 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("utf8mb4"); is just enough, let the mysqli db driver do the thing for you.