mysqli::prepare
(mysqli_prepare) Prepare a SQL statement for execution
The prepare()
/ mysqli_prepare()
function is used to prepare the SQL statement to be executed.
Prepare the SQL statement to be executed:
<?php $mysqli = new mysqli ( "localhost" , "my_user" , "my_password" , "my_db" ) ; // Check the connection if ( $mysqli -> connect_errno ) { echo "Failed to connect to MySQL: " . $mysqli -> connect_error ; exit ( ) ; } // Prepare and bind $stmt = $mysqli -> prepare ( "INSERT INTO MyGuests (firstname, lastname, email) VALUES (?, ?, ?)" ) ; $stmt -> bind_param ( "sss" , $firstname , $lastname , $email ) ; // Set parameters and execute $firstname = "John" ; $lastname = "Doe" ; $email = "john@example.com" ; $stmt -> execute ( ) ; $firstname = "Mary" ; $lastname = "Moe" ; $email = "mary@example.com" ; $stmt -> execute ( ) ; echo "New records created successfully" ; $stmt -> close ( ) ; $mysqli -> close ( ) ; ?>