Oracle 5.0 Reference Manual page 2256

Table of Contents

Advertisement

escaping mechanism (e.g.,
mysql_real_escape_string
will adhere to this setting. It is important to realize that these functions will not
use the character set that is defined with a query, so for example the following
will not have an effect on them:
Example 20.18. Problems with setting the character set with SQL
<?php
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");
// Will not affect $mysqli->real_escape_string();
$mysqli->query("SET NAMES utf8");
// Will not affect $mysqli->real_escape_string();
$mysqli->query("SET CHARACTER SET utf8");
// But, this will affect $mysqli->real_escape_string();
$mysqli->set_charset('utf8');
?>
Below are examples that demonstrate how to properly alter the character set at runtime using each
each API.
Example 20.19. Setting the character set example: mysqli
<?php
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");
if (!$mysqli->set_charset('utf8')) {
printf("Error loading character set utf8: %s\n", $mysqli->error);
} else {
printf("Current character set: %s\n", $mysqli->character_set_name());
}
print_r( $mysqli->get_charset() );
?>
Example 20.20. Setting the character set example:
Note: This only works as of PHP 5.3.6.
<?php
$pdo = new PDO("mysql:host=localhost;dbname=world;charset=utf8", 'my_user', 'my_pass');
?>
Example 20.21. Setting the character set example: mysql
<?php
$conn = mysql_connect("localhost", "my_user", "my_pass");
$db
= mysql_select_db("world");
if (!mysql_set_charset('utf8', $conn)) {
echo "Error: Unable to set the character set.\n";
exit;
Overview of the MySQL PHP drivers
mysqli_real_escape_string
2236
for mysql, and
PDO::quote
pdo_mysql
for mysqli,
for PDO_MySQL)

Advertisement

Table of Contents
loading

This manual is also suitable for:

Mysql 5.0

Table of Contents