Usage analysis of PHP mysqli extension library preprocessing techniques

  • 2020-05-17 06:41:22
  • OfStack

1. Add 3 users to the database using mysqli stmt extension library preprocessing technology


<?php
    //mysqli Extension libraries   Pretreatment technique  mysqli stmt  Add to the database 3 A user 
    //1 , create, mysqli object 
    $mysqli = new MySQLi("localhost","root","root","test");
    if($mysqli->connect_error){
        die($mysqli->conncet_error);
    }
    //2 Create a precompiled object 
    $sql="insert into user1(name,password,email,age) values(?,?,?,?)";
    $mysqli_stmt=$mysqli->prepare($sql);
    // Binding parameters 
    $name=" Small fang ";
    $password="123456";
    $email="xiaofang@126.com";
    $age=18;

    // Parameter binding -> Give? Assignment no.   Here the type and the order have to be 1 to 
    $mysqli_stmt->bind_param("sssi",$name,$password,$email,$age);
    // perform 
    $b=$mysqli_stmt->execute();
    // Continue to add 
    $name=" Xiao Yang ";
    $password="123456";
    $email="xiaoyang@126.com";
    $age=18;

    // Parameter binding -> Give? Assignment no.   Here the type and the order have to be 1 to 
    $mysqli_stmt->bind_param("sssi",$name,$password,$email,$age);
    // perform 
    $b=$mysqli_stmt->execute();    
    // Continue to add 
    $name=" small G";
    $password="123456";
    $email="xiaoG@126.com";
    $age=18;

    // Parameter binding -> Give? Assignment no.   Here the type and the order have to be 1 to 
    $mysqli_stmt->bind_param("sssi",$name,$password,$email,$age);
    // perform 
    $b=$mysqli_stmt->execute();    
    if(!$b){
        echo " The operation failure ".$mysqli_stmt->error;
    }else{
        echo " Operation is successful ";
    }
    // Turn off precompilation 
    $mysqli_stmt->close();
    $mysqli->close();
?>

2. Query id with preprocessing > 5 user id name email

<?php
    // Using preprocessed queries id>5 The user id name email
    $mysqli=new MySQLi("localhost","root","root","test");
    if($mysqli->connect_error){
        die($mysqli->connect_error);
    }
    // Create a precompiled object 
    $sql="select id,name,email from user1 where id>?";
    $mysqli_stmt=$mysqli->prepare($sql);

    $id=5;
    // Binding parameters 
    $mysqli_stmt->bind_param("i",$id);
    // Bind result set 
    $mysqli_stmt->bind_result($id,$name,$email);
    // perform 
    $mysqli_stmt->execute();
    // Fetches the value of the binding 
    while($mysqli_stmt->fetch()){
        echo "<br/>$id--$name--$email";
    }

    // Close the resource 
    // Release of the results 
    $mysqli_stmt->free_result();
    // Close and compile statements 
    $mysqli_stmt->close();
    // Close the connection 
    $mysqli->close();
?>


Related articles: