Creative, Informative and Entertaining Stuff for everyone

Monday February 6th 2012

Categories

Archives

Calender

February 2012
S M T W T F S
« Dec    
 1234
567891011
12131415161718
19202122232425
26272829  

How to fetch multi-lingual content from database

Bookmark and Share

The basic purpose of this post is to guide you all about fetching multilingual content from database.
I mean to say that, if you have stored japanese text in your database, So in order to fetch the japanese content , you need to set the character encoding to UTF-8 format, while communication with the database for fetching the result.

In Case of MYSQLi

$mysqli = new mysqli("localhost", "db_user", "db_password","db_name");

/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}

/* change character set to utf8 */
if (!mysqli_set_charset($link, "utf8")) {
printf("Error loading character set utf8: %s\n", mysqli_error($link));
} else {
printf("Current character set: %s\n", mysqli_character_set_name($link));
}

/* Select queries return a resultset */
if ($result = $mysqli->query(“SELECT ColumnName FROM tableName “)) {
printf(“Select returned %d rows.\n”, $result->num_rows);

/* free result set */
$result->close();
}
$mysqli->close();
?>

In case you are using multiple database

$link1 = mysqli_connect('localhost','user1','pass1',TRUE);
$link2 = mysqli_connect('localhost','user1','pass1',TRUE);

mysqli_select_db('db1',$link1);
mysqli_select_db('db2',$link2);

mysqli_set_charset('latin1',$link1);
mysqli_set_charset('utf8',$link2);
?>

If you don’t pass in “true” to mysqli_connect() in the example below, $link1 and $link2 will have the same resource id# and both database connections will end up being set to utf-8 charsets.

Leave a Reply