Home › PHP Tutorial › PHP MySQL prepared statements
PHP MySQL prepared statements
Talk to a database without SQL injection.
Connect with mysqli (or PDO). Never put user input straight into SQL. A prepared statement sends the query with ? placeholders first and binds the values separately, which blocks SQL injection. This needs a real MySQL server, so the output below is what you would see.
Example code
<?php
$conn = new mysqli("localhost", "user", "password", "myDB");
$stmt = $conn->prepare("INSERT INTO users (name, email) VALUES (?, ?)");
$stmt->bind_param("ss", $name, $email);
$name = "Ada";
$email = "ada@example.com";
$stmt->execute();
echo "New record created successfully";
$stmt->close();
$conn->close();
?>