English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

PHP Basic Tutorial

PHP Advanced Tutorial

PHP & MySQL

PHP Reference Manual

Usage and example of PHP mysqli_stmt_bind_param() function

PHP MySQLi Reference Manual

The mysqli_stmt_bind_param() function binds variables as parameters to the prepared statement.

Definition and usage

mysqli_stmt_bind_param()The function is used to bind variables to the parameters of a prepared statement.

Syntax

mysqli_stmt_bind_param($stmt, $types, $var1, $var2...);

Parameter

Serial numberParameters and descriptions
1

stmt (required)

This is the object representing the prepared statement.

2

types (required)

A string (consisting of a single character) used to specify the type of the variable, where:

  • i  Represents integer type

  • d  Represents double precision type

  • s  Represents string type

  • b Represents Blob type

3

var (required)

The value of the variable, separated by commas.

Return value

The PHP mysqli_stmt_bind_param() function returns a boolean value, true when successfultrue, on failure it isfalse.

PHP version

This function was originally in PHP version5introduced and can be used in all higher versions.

Online Example

The following example demonstratesmysqli_stmt_bind_param()Usage of the function (procedural style)-

<?php
   //Establish connection
   $con = new mysqli("localhost", "root", "password", "mydb");
   //Creating a table
   $con -> query("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");
   //Insert values into the table using prepared statements
   $stmt = $con -> prepare("INSERT INTO myplayers values(?, ?, ?, ?, ?");
   //Bind values to parameter markers
   $stmt -> bind_param("issss", $id, $fname, $lname, $pob, $country);
   $id = 1;
   $fname = 'Shikhar';
   $lname = 'Dhawan';
   $pob = 'Delhi';
   $country = 'India';
   //Execute statement
   $stmt->execute();
   //End statement
   $stmt->close();
   //Close connection
   $con->close();
?>

Output result

Create table.....

Online Example

In object-oriented style, the syntax of this function is$stmt-> close();.Here is an example of this function in object-oriented style;

<?php
   //Establish connection
   $con = new mysqli("localhost", "root", "password", "mydb");
   //Create table
   $con -> query("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");
   //Insert values into the table using prepared statements
   $stmt = $con -> prepare("INSERT INTO myplayers values(?, ?, ?, ?, ?");
   //Bind values to parameter markers
   $stmt -> bind_param("issss", $id, $fname, $lname, $pob, $country);
   $id = 1;
   $fname = 'Shikhar';
   $lname = 'Dhawan';
   $pob = 'Delhi';
   $country = 'India';
   //Execute statement
   $stmt->execute();
   //End statement
   $stmt->close();
   //Close connection
   $con->close();
?>

Output result

Create table.....

Online Example

Here is another example of this function-

<?php
   $con = @mysqli_connect("localhost", "root", "password", "mydb");
   mysqli_query($con, "CREATE TABLE Test(Name VARCHAR(255), AGE INT)");
   print("Creating table.....\n");
   mysqli_query($con, "insert into Test values('Raju', 25),('Rahman', 30),('Sarmista', 27);
   print("Inserting record.....\n");
   $stmt = mysqli_prepare($con, "DELETE FROM test WHERE Age<?");
   mysqli_stmt_bind_param($stmt, "i", $num);
   $num = 28;
   //Execute statement
   mysqli_stmt_execute($stmt);
   //End statement
   mysqli_stmt_close($stmt);
   //Close connection
   mysqli_close($con);
?>

Output result

Create table.....

PHP MySQLi Reference Manual