note 93707 modified in mysqli-stmt.store-result by danbrown
| From: | danbrown@php.net | Date: | Wed, 23 Sep 2009 20:22:11 +0000 |
| Subject: | note 93707 modified in mysqli-stmt.store-result by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-160996@lists.php.net to get a copy of this message | ||
The below statement is only true if you're using ::fetch_row(), which must be called on a
result object. Calling $stmt->store_result() effectively converts the statement object to a
result object, which you can then call $stmt->fetch_row() on. (I don't know if it's
actually converting the object from one type to another in the background, but that's the
functional outworking of it.)
If you use ::fetch() with a bound result, it is not necessary to call ::store_result.
So your two options look like this:
<?php
//Method 1: bound result
if ($stmt = $mysqli->prepare('SELECT name FROM employees')) {
$stmt->execute();
$stmt->bind_result($name);
while($stmt->fetch()) {
echo $name;
}
}
//Method 2: store_result
if ($stmt = $mysqli->prepare('SELECT name FROM employees')) {
$stmt->execute();
$stmt->store_result(); //"Converts" statement object to result object
while($row = $stmt->fetch_row()) {
echo $row[0];
}
}
?>
Another option is to use ::store_result() and ::fetch_array() instead of using a bound result. See
http://php.net/mysqli_fetch_assoc
--was--
The below statement is only true if you're using ::fetch_row(), which must be called on a
result object. Calling $stmt->store_result() effectively converts the statement object to a
result object, which you can then call $stmt->fetch_row() on. (I don't know if it's
actually converting the object from one type to another in the background, but that's the
functional outworking of it.)
If you use ::fetch() with a bound result, it is not necessary to call ::store_result.
So your two options look like this:
<?php
//Method 1: bound result
if ($stmt = $mysqli->prepare('SELECT name FROM employees')) {
$stmt->execute();
$stmt->bind_result($name);
while($stmt->fetch()) {
echo $name;
}
}
//Method 2: store_result
if ($stmt = $mysqli->prepare('SELECT name FROM employees')) {
$stmt->execute();
$stmt->store_result(); //"Converts" statement object to result object
while($row = $stmt->fetch_row()) {
echo $row[0];
}
}
?>
http://php.net/manual/en/mysqli-stmt.store-result.php