Spec-Zone .ru
спецификации, руководства, описания, API

22.9.1.5. Concepts

Copyright 1997-2012 the PHP Documentation Group.

These concepts are specific to the MySQL drivers for PHP.

22.9.1.5.1. Buffered and Unbuffered queries

Copyright 1997-2012 the PHP Documentation Group.

Queries are using the buffered mode by default. This means that query results are immediately transferred from the MySQL Server to PHP in is then kept in the memory of the PHP process. This allows additional operations like counting the number of rows, and moving (seeking) the current result pointer. It also allows issuing further queries on the same connection while working on the result set. The downside of the buffered mode is that larger result sets might require quite a lot memory. The memory will be kept occupied till all references to the result set are unset or the result set was explicitly freed, which will automatically happen during request end the latest. The terminology "store result" is also used for buffered mode, as the whole result set is stored at once.

Note

When using libmysqlclient as library PHP's memory limit won't count the memory used for result sets unless the data is fetched into PHP variables. With mysqlnd the memory accounted for will include the full result set.

Unbuffered MySQL queries execute the query and then return a resource while the data is still waiting on the MySQL server for being fetched. This uses less memory on the PHP-side, but can increase the load on the server. Unless the full result set was fetched from the server no further queries can be sent over the same connection. Unbuffered queries can also be referred to as "use result".

Following these characteristics buffered queries should be used in cases where you expect only a limited result set or need to know the amount of returned rows before reading all rows. Unbuffered mode should be used when you expect larger results.

Because buffered queries are the default, the examples below will demonstrate how to execute unbuffered queries with each API.

Example 22.15. Unbuffered query example: mysqli

<?php$mysqli  = new mysqli("localhost", "my_user", "my_password", "world");$uresult = $mysqli->query("SELECT Name FROM City", MYSQLI_USE_RESULT);if ($uresult) {   while ($row = $uresult->fetch_assoc()) {       echo $row['Name'] . PHP_EOL;   }}$uresult->close();?>

Example 22.16. Unbuffered query example: pdo_mysql

<?php$pdo = new PDO("mysql:host=localhost;dbname=world", 'my_user', 'my_pass');$pdo->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, false);$uresult = $pdo->query("SELECT Name FROM City");if ($uresult) {   while ($row = $uresult->fetch(PDO::FETCH_ASSOC)) {       echo $row['Name'] . PHP_EOL;   }}?>

Example 22.17. Unbuffered query example: mysql

<?php$conn = mysql_connect("localhost", "my_user", "my_pass");$db   = mysql_select_db("world");$uresult = mysql_unbuffered_query("SELECT Name FROM City");if ($uresult) {   while ($row = mysql_fetch_assoc($uresult)) {       echo $row['Name'] . PHP_EOL;   }}?>

22.9.1.5.2. Character sets

Copyright 1997-2012 the PHP Documentation Group.

Ideally a proper character set will be set at the server level, and doing this is described within the Character Set Configuration section of the MySQL Server manual. Alternatively, each MySQL API offers a method to set the character set at runtime.

The character set and character escaping

The character set should be understood and defined, as it has an affect on every action, and includes security implications. For example, the escaping mechanism (e.g., mysqli_real_escape_string for mysqli, mysql_real_escape_string for mysql, and PDO::quote for PDO_MySQL) 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 22.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 22.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 22.20. Setting the character set example:pdo_mysql

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 22.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;}echo 'Your current character set is: ' .  mysql_client_encoding($conn);?>