Fonction PHP mysqli_stmt_bind_param ()

Définition et utilisation

le mysqli_stmt_bind_param() La fonction est utilisée pour lier des variables aux marqueurs de paramètre d'une instruction préparée.

Syntaxe

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

Paramètres

Sr. Non Paramètre et description
1

stmt(Mandatory)

Il s'agit d'un objet représentant une instruction préparée.

2

types(Mandatory)

Une chaîne (de caractères individuels) spécifiant les types des variables où -

  • i représente un type entier

  • d représente un double type

  • s représente un type de chaîne

  • b représente un type de blob

3

var(Mandatory)

Valeurs des variables, séparées par des virgules.

Valeurs de retour

La fonction PHP mysqli_stmt_bind_param () renvoie une valeur booléenne qui est vraie en cas de succès et fausse en cas d'échec.

Version PHP

Cette fonction a été introduite pour la première fois dans la version 5 de PHP et fonctionne dans toutes les versions ultérieures.

Exemple

L'exemple suivant montre l'utilisation de la fonction mysqli_stmt_bind_param () (dans le style procédural) -

<?php
   //Creating a 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("Table Created.....\n");

   //Inserting values into the table using prepared statement
   $stmt = $con -> prepare( "INSERT INTO myplayers values(?, ?, ?, ?, ?)");

   //Binding values to the parameter markers
   $stmt -> bind_param("issss", $id, $fname, $lname, $pob, $country);
   $id = 1;
   $fname = 'Shikhar';
   $lname = 'Dhawan';
   $pob = 'Delhi';
   $country = 'India';

   //Executing the statement
   $stmt->execute();
   //Closing the statement
   $stmt->close();
   //Closing the connection
   $con->close();
?>

Cela produira le résultat suivant -

Table Created.....

Exemple

Dans le style orienté objet, la syntaxe de cette fonction est $ stmt-> close (); Voici l'exemple de cette fonction dans le style orienté objet $ minus;

<?php
   //Creating a 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("Table Created.....\n");

   //Inserting values into the table using prepared statement
   $stmt = $con -> prepare( "INSERT INTO myplayers values(?, ?, ?, ?, ?)");

   //Binding values to the parameter markers
   $stmt -> bind_param("issss", $id, $fname, $lname, $pob, $country);
   $id = 1;
   $fname = 'Shikhar';
   $lname = 'Dhawan';
   $pob = 'Delhi';
   $country = 'India';

   //Executing the statement
   $stmt->execute();

   //Closing the statement
   $stmt->close();

   //Closing the connection
   $con->close();
?>

Cela produira le résultat suivant -

Table Created.....

Exemple

Voici un autre exemple de cette fonction -

<?php
   $con = @mysqli_connect("localhost", "root", "password", "mydb");

   mysqli_query($con, "CREATE TABLE Test(Name VARCHAR(255), AGE INT)");
   print("Table Created.....\n");
   mysqli_query($con, "insert into Test values('Raju', 25),('Rahman', 30),('Sarmista', 27)");
   print("Records Inserted.....\n");

   $stmt = mysqli_prepare($con, "DELETE FROM test where Age<?");
   mysqli_stmt_bind_param($stmt, "i", $num);
   $num = 28;
   //Executing the statement
   mysqli_stmt_execute($stmt);

   //Closing the statement
   mysqli_stmt_close($stmt);
   //Closing the connection
   mysqli_close($con);
?>

Cela produira le résultat suivant -

Table Created.....