php-mysql connection charset fix

view a random?

In my earlier post php-mysql connection charset fix, I mentioned I was having issues with enforcing an UTF8 charset. I found the solution:

@$conn = new mysqli(DB_SERVER, DB_USER, DB_PASS, DB_NAME);
// or die(mysql_error());
if (!$conn||mysqli_connect_errno())
{
throw new Exception('Could not connect to database server:'.
mysqli_connect_error());
}
else
{
$conn->query( "SET NAMES 'utf8'" );
$conn->set_charset("utf8");
return $conn;
}

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Facebook
  • Google
  • MisterWong
  • Reddit
  • Scoopit
  • StumbleUpon
  • Technorati
  • rss

One Trackback

  1. […] and then each table and finally each column, the connection still defaults to latin-1. I provide the solution in a later post and below for your Google searching convenience. @$conn = new mysqli(DB_SERVER, […]

Post a Comment

You must be logged in to post a comment.