Skip to content

Instantly share code, notes, and snippets.

@saturngod
Created July 6, 2011 05:46
Show Gist options
  • Save saturngod/1066641 to your computer and use it in GitHub Desktop.
Save saturngod/1066641 to your computer and use it in GitHub Desktop.
mysql with utf8 in PHP
<?php
//mysql init
mysql_connect('localhost', 'root', 'password');
mysql_select_db('mydb');
//Init for UTF8
mysql_query("SET NAMES utf8");
/// Now Start my code
$query = "SELECT `Title` FROM `album` WHERE id=1";
// Perform Query
$result = mysql_query($query);
// Check result
// This shows the actual query sent to MySQL, and the error. Useful for debugging.
if (!$result) {
$message = 'Invalid query: ' . mysql_error() . "\n";
$message .= 'Whole query: ' . $query;
die($message);
}
// Use result
// Attempting to print $result won't allow access to information in the resource
// One of the mysql result functions must be used
// See also mysql_result(), mysql_fetch_array(), mysql_fetch_row(), etc.
while ($row = mysql_fetch_assoc($result)) {
echo $row['Title'];
echo "<br/>";
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment