SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Addict
    Join Date
    Nov 2009
    Posts
    312
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Database connection using mysqli with charset

    Hi,

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

    PHP Code:
    $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:

    PHP Code:
    $db = new mysqli($db_host$db_user$db_pass$db_name);
    $db->set_charset("utf8"); 
    Thanks.

  2. #2
    SitePoint Wizard
    Join Date
    Oct 2005
    Posts
    1,851
    Mentioned
    5 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by nayen View Post
    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:

    PHP Code:
    $db = new mysqli($db_host$db_user$db_pass$db_name);
    $db->set_charset("utf8"); 
    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:

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


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •