Skip to content

Instantly share code, notes, and snippets.

@shashank-p
Created October 29, 2020 07:48
Show Gist options
  • Save shashank-p/9519aa9dc0fa7ea448448c73114c0e9d to your computer and use it in GitHub Desktop.
Save shashank-p/9519aa9dc0fa7ea448448c73114c0e9d to your computer and use it in GitHub Desktop.
Prepared Statements in PHP for CRUD
<?php
$servername = "localhost";
$username = "root";
$password = " ";
$dbname = "demo";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
/* ------------------------------------------------------------------------------------------------------------------------------------------ */
/*
Binds variables to prepared statement
i corresponding variable has type integer
d corresponding variable has type double
s corresponding variable has type string
b corresponding variable is a blob and will be sent in packets
*/
/* ------------------------------------------------------------------------------------------------------------------------------------------ */
// Insert data
$stmt = $conn->prepare("INSERT INTO `myguests`(`firtname`, `lastname`, `email`) VALUES ( ?, ?, ?)");
$stmt->bind_param("sss", $firstname, $lastname, $email);
// insert a row
$firstname = "John";
$lastname = "Doe";
$email = "[email protected]";
$stmt->execute();
echo "New records created successfully <br><br>";
$stmt->free_result();
$stmt->close();
/* ------------------------------------------------------------------------------------------------------------------------------------------ */
// Update data
$stmt2 = $conn->prepare("UPDATE `myguests` SET `firtname`=?,`lastname`=?,`email`=? WHERE `id`=?");
$stmt2->bind_param("sssi", $firstname, $lastname, $email, $id);
// update a row
$firstname = "Johnny";
$lastname = "Depp";
$email = "[email protected]";
$id = 8;
$stmt2->execute();
echo "Records updated successfully <br><br>";
$stmt2->free_result();
$stmt2->close();
/* ------------------------------------------------------------------------------------------------------------------------------------------ */
//Display the result
$stmt1 = $conn->prepare("SELECT * FROM `myguests`");
//$stmt1->bind_param('s', $id);
$stmt1->execute();
$stmt1->bind_result($id, $firstname, $lastname, $email);
$stmt1->store_result();
if ($stmt1->num_rows() > 0) {
while ($stmt1->fetch()) {
echo $id . ") " . $firstname . " " . $lastname . " " . $email . "<br><br>";
}
}
$stmt1->free_result();
$stmt1->close();
/* ------------------------------------------------------------------------------------------------------------------------------------------ */
//Delete the result
$stmt3 = $conn->prepare("DELETE FROM `myguests` WHERE `id`=?");
$stmt3->bind_param('i', $id);
$id = 16;
$stmt3->execute();
$stmt3->free_result();
$stmt3->close();
/* ------------------------------------------------------------------------------------------------------------------------------------------ */
$conn->close();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment