English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
The mysqli_stmt_bind_result() function binds variables to the prepared statement to store the results
mysqli_stmt_bind_result()The function is used to bind the columns of the result set to variables. After binding the variables, you need to call mysqli_stmt_fetch() The function is used to get the value of the columns in the specified variable.
mysqli_stmt_bind_result($stmt, $var1, $var2...);
Serial number | Parameters and descriptions |
---|---|
1 | stmt(required) This is the object representing the prepared statement. |
2 | var1(required) This indicates the variables to be bound to the columns. |
The PHP mysqli_stmt_bind_result() function returns a boolean value, true on success.true, and is false on failure.false.
This function was originally introduced in PHP version5introduced and can be used in all higher versions.
The following examples demonstratemysqli_stmt_bind_result()The usage of the function (procedural style), binds the result to a variable:
<?php $con = mysqli_connect("localhost", "root", "password", "mydb"); mysqli_query($con, "CREATE TABLE myplayers(ID INT, First_Name VARCHAR(255), Last_Name VARCHAR(255), Place_Of_Birth VARCHAR(255), Country VARCHAR(255))"); print("Create table.....\n"); mysqli_query($con, "INSERT INTO myplayers values(1, 'Sikhar', 'Dhawan', 'Delhi', 'India')"; mysqli_query($con, "INSERT INTO myplayers values(2, 'Jonathan', 'Trott', 'CapeTown', 'SouthAfrica')"; mysqli_query($con, "INSERT INTO myplayers values(3, 'Kumara', 'Sangakkara', 'Matale', 'Srilanka')"); print("Inserting record.....\n"); //Retrieve the content of the table $stmt = mysqli_prepare($con, "SELECT * FROM myplayers"); //Execute Statement mysqli_stmt_execute($stmt); //Bind the values in the result to the variables mysqli_stmt_bind_result($stmt, $id, $fname, $lname, $pob, $country); while (mysqli_stmt_fetch($stmt)) { print("Id: ".$id."\n"); print("fname: ".$fname."\n"); print("lname: ".$lname."\n"); print("pob: ".$pob."\n"); print("country: ".$country."\n"); print("\n"); } //End Statement mysqli_stmt_close($stmt); //Close Connection mysqli_close($con); ?>
Output Result
Creating table..... Inserting record..... Id: 1 fname: Sikhar lname: Dhawan pob: Delhi country: India Id: 2 fname: Jonathan lname: Trott pob: CapeTown country: SouthAfrica Id: 3 fname: Kumara lname: Sangakkara pob: Matale country: Srilanka
In the object-oriented style, the syntax of this function is$stmt-> bind_result();。Below is an example of this function in an object-oriented style, binding variables to the result set:
<?php //Establishing connection $con = new mysqli("localhost", "root", "password", "mydb"); $con -> query("CREATE TABLE Test(Name VARCHAR(255), AGE INT); $con -> query("insert into Test values('Raju', 25),('Rahman', 30),('Sarmista', 27); print("Create table.....\n"); $stmt = $con -> prepare("SELECT * FROM Test WHERE Name in(?, ?); $stmt -> bind_param("ss", $name)1, $name2); $name1 = 'Raju'; $name2 = 'Rahman'; print("Records Deleted.....\n"); //Execute Statement $stmt->execute(); //Bind variables to the result set $stmt->bind_result($name, $age); while ($stmt->fetch()) { print("Name: ".$name." "); print("Age: ".$age." "); } //End Statement $stmt->close(); //Close Connection $con->close(); ?>
Output Result
Creating table..... Records Deleted..... Name: Raju Age: 25 Name: Rahman Age: 30
The following example uses the mysqli_stmt_bind_result() and mysqli_stmt_fetch() functions to retrieve the results of a DESCRIBE query and bind the values in the results to variables:
<?php $con = mysqli_connect("localhost", "root", "password", "mydb"); mysqli_query($con, "CREATE TABLE myplayers(ID INT, First_Name VARCHAR(255), Last_Name VARCHAR(255), Place_Of_Birth VARCHAR(255), Country VARCHAR(255))"); print("Create table.....\n"); //Description of the table $stmt = mysqli_prepare($con, "DESC myplayers"); //Execute Statement mysqli_stmt_execute($stmt); //Bind the values in the result to the variables mysqli_stmt_bind_result($stmt, $field, $type, $null, $key, $default, $extra); while (mysqli_stmt_fetch($stmt)) { print("Field: ".$field." "); print("Type: ".$type." "); print("Null: ".$null." "); print("Key: ".$key." "); print("Default: ".$default." "); print("Extra: ".$extra." "); print("\n"); } //End Statement mysqli_stmt_close($stmt); //Close Connection mysqli_close($con); ?>
Output Result
Creating table..... Field: ID Type: int(11) Null: YES Key: Default: Extra: Field: First_Name Type: varchar(255) Null: YES Key: Default: Extra: Field: Last_Name Type: varchar(255) Null: YES Key: Default: Extra: Field: Place_Of_Birth Type: varchar(255) Null: YES Key: Default: Extra: Field: Country Type: varchar(255) Null: YES Key: Default: Extra:
The following example uses the mysqli_stmt_bind_result() and mysqli_stmt_fetch() functions to obtain the results of the SHOW TABLES query, listing all the tables in the database:
<?php $con = mysqli_connect("localhost", "root", "password"); //Selecting the database mysqli_query($con, "CREATE DATABASE NewDatabase"); mysqli_select_db($con, "NewDatabase"); //Creating tables mysqli_query($con, "CREATE TABLE test1(Name VARCHAR(255), Age INT)"); mysqli_query($con, "CREATE TABLE test2(Name VARCHAR(255), Age INT)"); mysqli_query($con, "CREATE TABLE test3(Name VARCHAR(255), Age INT)"); print("Tables Created.....\n"); //Table Description $stmt = mysqli_prepare($con, "SHOW TABLES"); //Execute Statement mysqli_stmt_execute($stmt); //Bind the values in the result to the variables mysqli_stmt_bind_result($stmt, $table_name); print("The following tables are all in the current database: \n"); while (mysqli_stmt_fetch($stmt)) { print($table_name."\n"); } //End Statement mysqli_stmt_close($stmt); //Close Connection mysqli_close($con); ?>
Output Result
Tables Created..... The following tables are all in the current database: test1 test2 test3