The prepared statement is a special function embedded in PHP which allows programmers to write codes that can be executed multiple times in an efficient manner by our database. When communicating with your database, extra care is given to security, query time, and server request size. With prepared statements, these factors are considered.
First, there is an initial preparation of the SQL template.
Second, we initialize the preparation using the mysqli_stmt_init()
function which passes an argument to the database connection.
mysqli_stmt_prepare()
function, the database connection is passed as the first argument.Whenever the values are provided, they are bound to the template variables and the database executes the command.
This is achieved when the mysqli_stmt_bind_param()
and the mysqli_stmt_execute()
functions pass the relevant parameters.
mysqli_stmt_get_result()
function to pass the initialization variable as an argument.mysqli_stmt_get_result()
function with the initialization variable as an argument.In our examples, we have created:
myDatabase
with $dbcon
as the connection variable.myTable
, which has columns with the names email
and username
from which we would make a selection of rows.With that setup we can get started.
<?php$nameOfServer = "localhost";$serverUsername = "username";$password = "password";$databaseName = "myDatabase";// Create connection$dbcon = mysqli_connect($nameOfServer, $serverUsername, $password, $databaseName);// Check connectionif (!$dbcon) {die("Connection failed: " . mysqli_connect_error());}//prepare sql template using "?" as placeholders.$query = "SELECT * FROM myTable WHERE username = ? OR email = ?; ";// initialize database connection.$bindstmt = mysqli_stmt_init($dbcon);//prepare the query and check if successfulif(!mysqli_stmt_prepare($bindstmt,$query)){echo "stmt failed";exit();}//Now bin the parameters if preparation was a success.mysqli_stmt_bind_param($bindstmt,'ss',$username, $email);//execute the statement.mysqli_stmt_execute($bindstmt);//get the result and fetch it as an array$stmtresult = mysqli_stmt_get_result($bindstmt);if($row = mysqli_fetch_assoc($stmtresult)){return $row;}else{$result = false;return $result;}//Now close the statement and db connectionmysqli_stmt_close($bindstmt);mysqli_close($dbcon);}?>
In this example, the assumptions and declared variables will be used, but we will do an insertion rather than a selection.
<?php$nameOfServer = "localhost";$serverUsername = "username";$password = "password";$databaseName = "myDatabase";// Create connection$dbcon = new mysqli($nameOfServer, $serverUsername, $password, $databaseName);// Check connectionif ($conn->connect_error) {die("Connection failed: " . $conn->connect_error);}// prepare and bind$stmt = $conn->prepare("INSERT INTO myTable (fullname, username, email) VALUES (?, ?, ?)");$stmt->bind_param("sss", $fullname, $username, $email);// set parameters and execute$fullname = "mary john";$username = "marrywise";$email = "mary@wise.com";$stmt->execute();//this will insert the values into our table/*if the value to be inserted was from a formyou would have to replace our static values herewith the values of the form input if it is set .*/$stmt->close();$dbcon->close();;?>