Podręcznik PHP
Poprzedni |  | Następny

# mysqli_bind_result

(PHP 5 CVS only)

mysqli_bind_result

(no version information, might be only in CVS)

stmt->bind_result -- Binds variables to a prepared statement for result storage

## Description

Procedural style:

bool mysqli_bind_result ( object stmt, mixed var1 [, mixed var2, ...])

Object oriented style (method):

class stmt {
bool bind_result ( mixed var1 [, mixed var2, ...])
}

mysqli_bind_result() is used to associate (bind) columns in the result set to variables. When mysqli_fetch() is called to fetch data, the MySQL client/server protocol places the data for the bound columns into the specified variables `var1, ...`.

> Notatka: Note that all columns must be bound prior to calling mysqli_fetch(). Depending on column types bound variables can silently change to the corresponding PHP type.

> A column can be bound or rebound at any time, even after a result set has been partially retrieved. The new binding takes effect the next time mysqli_fetch() is called.

## Return values

Zwraca `TRUE` w przypadku sukcesu, `FALSE` w przypadku porażki.

## See also

mysqli_bind_param(), mysqli_execute(), mysqli_fetch(), mysqli_prepare(), mysqli_stmt_errno(), mysqli_stmt_error()

## Example

Przykład 1. Object oriented style

`<?php
$mysqli = new mysqli("localhost", "my_user", "my_password", "test");
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
/* create table and insert some data */
$mysqli->query("DROP TABLE IF EXISTS bind_result");
$mysqli->query("CREATE TABLE bind_result (id int, extension varchar(20))");
$mysqli->query("INSERT INTO bind_result VALUES (1, 'ldap')");
$mysqli->query("INSERT INTO bind_result VALUES (2, 'mysql')");
$mysqli->query("INSERT INTO bind_result VALUES (3, 'pcre')");
/* prepare statement */
if ($stmt = $mysqli->prepare("SELECT id, extension FROM bind_result")) {
$stmt->execute();
/* bind variables to prepared statement */
$stmt->bind_result($col1, $col2);
/* fetch values */
while ($stmt->fetch()) {
printf("Id: %d Extension: %s\n", $col1, $col2);
}
/* close statement */
$stmt->close();
}
/* close connection */
$mysqli->close();
?>`

Przykład 2. Procedural style

`<?php
$link = mysqli_connect("localhost", "my_user", "my_password", "test")
or die("Can't connect to MySQL server on localhost");
/* create table and insert some data */
mysqli_query($link, "DROP TABLE IF EXISTS bind_result");
mysqli_query($link, "CREATE TABLE bind_result (id int, extension varchar(20))");
mysqli_query($link, "INSERT INTO bind_result VALUES (1, 'ldap')");
mysqli_query($link, "INSERT INTO bind_result VALUES (2, 'mysql')");
mysqli_query($link, "INSERT INTO bind_result VALUES (3, 'pcre')");
/* prepare statement */
if ($stmt = mysqli_prepare($link, "SELECT id, extension FROM bind_result")) {
mysqli_execute($stmt);
/* bind variables to prepared statement */
mysqli_bind_result($stmt, $col1, $col2);
/* fetch values */
while (mysqli_fetch($stmt)) {
printf("Id: %d Extension: %s\n", $col1, $col2);
}
/* close statement */
mysqli_stmt_close($stmt);
}
/* close connection */
mysqli_close($link);
?>`

Poprzedni | Spis treści | Następny
mysqli_bind_param | Początek rozdziału | mysqli_change_user