Skip to content

Instantly share code, notes, and snippets.

@kirilkirkov
Created July 27, 2015 14:31
Show Gist options
  • Save kirilkirkov/f2719db25c830a058471 to your computer and use it in GitHub Desktop.
Save kirilkirkov/f2719db25c830a058471 to your computer and use it in GitHub Desktop.
Example about prepare statements. No SQL Injections... no bugs, be safe
<?php
$servername = "localhost";
$username = "root";
$password = "toor";
$dbname = "test";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// prepare and bind
$stmt = $conn->prepare("INSERT INTO MyGuests (firstname, lastname, email) VALUES (?, ?, ?)");
$stmt->bind_param("sss", $firstname, $lastname, $email);
// set parameters and execute
$firstname = "John";
$lastname = "Doe";
$email = "[email protected]";
$stmt->execute();
$firstname = "Mary";
$lastname = "Moe";
$email = "[email protected]";
$stmt->execute();
$firstname = "Julie";
$lastname = "Dooley";
$email = "[email protected]";
$stmt->execute();
echo "Records are inserted successful !";
$stmt->close();
$conn->close();
?>
@kirilkirkov
Copy link
Author

The above example is Prepared Statements in MySQLi

The argument (in $stmt->bind_param ...) may be one of four types:

  • i - integer
  • d - double
  • s - string
  • b - BLOB

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment