English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
The mysqli_stmt_fetch() function extracts the results in the pre-prepared statement into the bound variables.
You can create a prepared statement with parameter markers (“?”) (if there is a value) using the mysqli_prepare() function. After the prepared statement, you need to usemysqli_stmt_bind_param()The function binds values to the parameters of the created statement.
In the same way, you can use the mysqli_stmt_bind_result() function to bind the columns of the result set of the statement to the required variables.
If calledmysqli_stmt_fetch();The function will extract the column of the result to the specified variable after binding the column.
mysqli_stmt_fetch($stmt);
Serial number | Parameters and descriptions |
---|---|
1 | stmt(Required) This represents the object of the prepared statement. |
If data is fetched, the PHP mysqli_stmt_fetch() function will returnTRUE;; if an error occurs, it returnsFALSE; if there are no more rows in the result, it returnsNULL.
This function was originally introduced in PHP version5introduced and can be used in all higher versions.
The following example demonstratesmysqli_stmt_fetch();Usage of the function (procedural style), binding the values in the result to variables using prepared statements:
<?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("Creating table.....\n"); mysqli_query($con, "INSERT INTO myplayers values(1, 'Sikhar', 'Dhawan', 'Delhi', 'India')"); mysqli_query($con, "INSERT INTO myplayers values(2, 'Jonathan', 'Trott', 'Cape Town', 'South Africa')"); 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 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
Create table..... Inserting record..... Id: 1 fname: Sikhar lname: Dhawan pob: Delhi country: India Id: 2 fname: Jonathan lname: Trott pob: Cape Town country: South Africa
In the object-oriented style, the syntax of this function is$stmt->fetch();.Here is an example of this function in an object-oriented style, using prepared statements to bind 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("Creating table.....\n"); $stmt = $con -> prepare("SELECT * FROM Test WHERE Name in(?, ?) $stmt -> bind_param("ss", $name1, $name2); $name1 = \'Raju\'; $name2 = \'Rahman\'; print("Record deleted.....\n"); //Execute statement $stmt->execute(); //Bind variables to the result set $stmt->bind_result($name, $age); while ($stmt->fetch()) { print("Name: \t".$name."\n"); print("Age: \t".$age."\n"); } //End statement $stmt->close(); //Close connection $con->close(); ?>
Output result
Create table..... Record deleted..... Name: \tRaju Age: 25 Name: \tRahman Age: 30
The following example uses mysqli_stmt_bind_result() and mysqli_stmt_fetch() functions to retrieve the results of a DESCRIBE query-
<?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("Creating table.....\n"); //Table description $stmt = mysqli_prepare($con, \"DESC myplayers\"); //Execute statement mysqli_stmt_execute($stmt); //Bind the values in the result to variables mysqli_stmt_bind_result($stmt, \t$field, \t$type, \t$null, \t$key, \t$default, \t$extra); while (mysqli_stmt_fetch($stmt)) { print("Field: \t".$field."\n"); print("Type: \t".$type."\n"); print("Null: \t".$null."\n"); print("Key: \t".$key."\n"); print("Default: ".$default."\n"); print("Extra: ".$extra."\n"); print("\n"); } //End statement mysqli_stmt_close($stmt); //Close connection mysqli_close($con); ?>
Output result
Create 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 mysqli_stmt_bind_result() and mysqli_stmt_fetch() functions to obtain the results of the SHOW TABLES query, returning all tables in the current database:
<?php $con = mysqli_connect("localhost", "root", "password"); //Select database mysqli_query($con, "CREATE DATABASE NewDatabase"); mysqli_select_db($con, "NewDatabase"); //Create table 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"); //Display tables $stmt = mysqli_prepare($con, "SHOW TABLES"); //Execute statement mysqli_stmt_execute($stmt); //Bind the values in the result to variables mysqli_stmt_bind_result($stmt, $table_name); print("All tables 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..... All tables in the current database: test1 test2 test3