Example 20.90. Less round trips using multi-INSERT SQL
<?php
if (!$mysqli->query("INSERT INTO test(id) VALUES (1), (2), (3), (4)")) {
echo "Multi-INSERT failed: (" . $mysqli->errno . ") " . $mysqli->error;
}
?>
Result set values data types
The MySQL Client Server Protocol defines a different data transfer protocol for prepared statements
and non-prepared statements. Prepared statements are using the so called binary protocol. The
MySQL server sends result set data "as is" in binary format. Results are not serialized into strings
before sending. The client libraries do not receive strings only. Instead, they will receive binary data
and try to convert the values into appropriate PHP data types. For example, results from an SQL
column will be provided as PHP integer variables.
Example 20.91. Native datatypes
<?php
$mysqli = new mysqli("example.com", "user", "password", "database");
if ($mysqli->connect_errno) {
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}
if (!$mysqli->query("DROP TABLE IF EXISTS test") ||
!$mysqli->query("CREATE TABLE test(id INT, label CHAR(1))") ||
!$mysqli->query("INSERT INTO test(id, label) VALUES (1, 'a')")) {
echo "Table creation failed: (" . $mysqli->errno . ") " . $mysqli->error;
}
$stmt = $mysqli->prepare("SELECT id, label FROM test WHERE id = 1");
$stmt->execute();
$res = $stmt->get_result();
$row = $res->fetch_assoc();
printf("id = %s (%s)\n", $row['id'], gettype($row['id']));
printf("label = %s (%s)\n", $row['label'], gettype($row['label']));
?>
The above example will output:
id = 1 (integer)
label = a (string)
This behavior differs from non-prepared statements. By default, non-prepared statements return all
results as strings. This default can be changed using a connection option. If the connection option is
used, there are no differences.
Fetching results using bound variables
Results from prepared statements can either be retrieved by binding output variables, or by requesting
a
object.
mysqli_result
Output variables must be bound after statement execution. One variable must be bound for every
column of the statements result set.
Example 20.92. Output variable binding
MySQL Improved Extension (Mysqli)
2320
INT
Need help?
Do you have a question about the 5.0 and is the answer not in the manual?
Questions and answers