Database connection using mysqli with charset

Hi,

In the past I used the following to connect to the database and set its charset:

$db = mysql_connect($db_host, $db_user, $db_pass);
mysql_select_db($db_name);
mysql_set_charset('utf8', $db);

Since mysql_ is deprecated, I am changing my code to mysqli_. I have the following and wanted to make sure if it is the correct way to do:

$db = new mysqli($db_host, $db_user, $db_pass, $db_name);
$db->set_charset("utf8");

Thanks.

That is the way I have it.

That is an important step many miss in addition to ensuring the text database table fields are set to UTF-8 (usually utf8_general_ci), and that HTML, CSS, JS, PHP, and other script files are saved in UTF-8 without BOM (byte order mark) format by whatever editor is used. Another good thing to do is to set a UTF-8 HTTP header early in your script before any content is output. Like this:

header('Content-type: text/html; charset=utf-8');