Tuesday, December 12, 2017

Change the character set of mysql connection in php


   Using the code we can change the character set of mysql in php for supporting  the utf8 character .


<?php

$mysql_host = 'localhost';
$mysql_username = 'your_database_user_name';
$mysql_password = 'your_password';
$mysql_database = 'your_database_name';
$link = mysqli_connect('localhost', $mysql_username, $mysql_password, $mysql_database);

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

printf("Initial character set: %s\n", mysqli_character_set_name($link));

//change character set to utf8

if (!mysqli_set_charset($link, "utf8")) {
    printf("Error loading character set utf8: %s\n", mysqli_error($link));
    exit();
} else {
    printf("<br><br>character set After Change: %s\n", mysqli_character_set_name($link));
}

mysqli_close($link);
?>


Output of this code :

Initial character set: latin1

character set After Change: utf8 

No comments:

Post a Comment

Featured Post

What is JavaScript? What is the role of JavaScript engine?

  The JavaScript is a Programming language that is used for converting static web pages to interactive and dynamic web pages. A JavaScript e...