UTF8 mysqli_connect code, retrieve/insert data as UTF8?

UTF8 mysqli_connect code, retrieve/insert data as UTF8?

Here is the code that you need to put in config.php.

After that, all the data that will be retrieved/inserted/updated from/in your SQL database will be converted to UTF8.

You can even have table rows set as non UTF8, all the data will be converted into UTF8.

This is the mysqli_connect version, the one you need.

1
2
3
4
5
6
7
8
9
10
11
12
<?php
$db_address = "localhost";
$db_name = "db name";
$db_username = "user";
$db_password = "password";
 
$connection = mysqli_connect($db_address, $db_username, $db_password, $db_name);
if($selectdb = mysqli_select_db($connection, $db_name)) {
  $sql = "SET NAMES 'utf8'";
  mysqli_query($connection, $sql);
}
?>

Add a comment: