PHP MySQL Prepared Statements
In this page:
$stmt = mysqli_prepare($conn, "SELECT * FROM table_name WHERE column = ?");
mysqli_stmt_bind_param($stmt, "s", $value); // s = string, i = integer
mysqli_stmt_execute($stmt);
Prepared Statements क्यों Exist करते हैं
किसी variable को query string में directly concatenate करके SQL बनाना, जैसे "SELECT * FROM users WHERE name = '$name'", एक attacker को '; DROP TABLE users; -- जैसी एक value supply करने देता है जो query का actual meaning बदल देती है।
Prepared statements SQL template और values को दो पूरी तरह अलग चीज़ों की तरह भेजकर इसे रोकते हैं।
उदाहरण: Why Prepared Statements Exist
<?php
$name = "Robert'); DROP TABLE users; --";
// Never do: "SELECT * FROM users WHERE name = '$name'"
echo "The SQL template and the value are sent separately instead";
?>
Login to try C/C++/Java/PHP code in the editor
Placeholders के साथ एक Statement Prepare करना
mysqli_prepare($conn, $sql) actual values की जगह एक या ज़्यादा ? placeholders वाला एक SQL statement compile करता है, एक statement object return करते हुए जिसे आप real values attach करके फिर execute करेंगे -- placeholders exactly बताते हैं कि values कहाँ जाएँगी बिना अभी यह specify किए कि वे क्या हैं।
उदाहरण: Preparing a Statement with Placeholders
<?php
$db = new SQLite3(':memory:');
$db->exec("CREATE TABLE users (name TEXT)");
$stmt = $db->prepare("INSERT INTO users (name) VALUES (?)"); // mysqli_prepare($conn, $sql)
echo "Statement compiled with a placeholder";
?>
Login to try C/C++/Java/PHP code in the editor
bind_param से Values Bind करना
mysqli_stmt_bind_param($stmt, $types, ...$values) actual values को एक prepared statement के placeholders से order में attach करता है -- $types string MySQL को हर value की kind बताती है: integer के लिए "i", double/float के लिए "d", string के लिए "s", और binary blob data के लिए "b"।
उदाहरण: Binding Values with bind_param
<?php
$db = new SQLite3(':memory:');
$db->exec("CREATE TABLE users (id INTEGER, name TEXT)");
$stmt = $db->prepare("INSERT INTO users (id, name) VALUES (?, ?)");
$stmt->bindValue(1, 1, SQLITE3_INTEGER); // like "i" in mysqli's bind_param
$stmt->bindValue(2, "Alice", SQLITE3_TEXT); // like "s"
$stmt->execute();
echo "Values bound by type";
?>
Login to try C/C++/Java/PHP code in the editor
Execute करना और Results Retrieve करना
mysqli_stmt_execute($stmt) prepared statement को इसकी bound values के साथ चलाता है, और mysqli_stmt_get_result($stmt) statement के result को एक regular result set में convert करता है जिस पर आप plain query के लिए इस्तेमाल किए गए same fetch functions से loop चला सकते हैं, जैसे mysqli_fetch_assoc()।
उदाहरण: Executing and Retrieving Results
<?php
// Create a new `SQLite3` instance with ':memory:', stored in `$db`
$db = new SQLite3(':memory:');
$db->exec("CREATE TABLE users (name TEXT)");
$db->exec("INSERT INTO users (name) VALUES ('Alice')");
// Declare `$stmt`, set to `$db->prepare("SELECT * FROM users WHERE name = ?")`
$stmt = $db->prepare("SELECT * FROM users WHERE name = ?");
$stmt->bindValue(1, "Alice", SQLITE3_TEXT);
// Declare `$result`, set to `$stmt->execute()`
$result = $stmt->execute();
// Print a human-readable dump of `$result->fetchArray(SQLITE3_ASSOC)`
print_r($result->fetchArray(SQLITE3_ASSOC));
?>
Login to try C/C++/Java/PHP code in the editor
एक Prepared Statement को Reuse करना
एक single prepared statement को हर बार re-prepare किए बिना अलग-अलग bound values के साथ कई बार execute किया जा सकता है -- हर नए set of values के लिए बस bind_param (या rebind) call करें और फिर execute करें, जो प्रति call एक fresh statement prepare करने से safer और तेज़ दोनों है।
उदाहरण: Reusing a Prepared Statement
<?php
$db = new SQLite3(':memory:');
$db->exec("CREATE TABLE users (name TEXT)");
$stmt = $db->prepare("INSERT INTO users (name) VALUES (?)");
foreach (["Alice", "Bob", "Carol"] as $name) {
$stmt->bindValue(1, $name, SQLITE3_TEXT);
$stmt->execute(); // same statement, new values each time
}
echo "3 rows inserted with one prepared statement";
?>
Login to try C/C++/Java/PHP code in the editor
- यह सोचकर किसी query के लिए prepared statements skip कर देना कि input 'safe लगता है', जबकि किसी user से आई, indirectly भी, कोई value untrusted मानी जानी चाहिए।
- bind_param call करते समय सही type string (s, i, d, b) specify करना भूल जाना, values को गलत तरीके से interpret होने का कारण बनाते हुए।
- उसी query में manually escaped, concatenated SQL को prepared statement placeholders के साथ mix करना, prepared statements द्वारा दी जाने वाली safety को defeat करते हुए।
- mysqli_prepare($conn, $sql) एक SQL statement compile करता है जिसमें values जाने वाली जगह ? placeholders होते हैं, कोई actual data attach होने से पहले।
- mysqli_stmt_bind_param($stmt, $types, ...$values) placeholders से real values attach करता है, हर value की kind बताने वाली एक type string के साथ।
- Database engine SQL structure और bound values को strictly अलग रखता है, bound values के through SQL injection को structurally असंभव बनाते हुए।
Chapter Quiz — Complete all 21 topics to unlock
0/21 topics done
Complete these topics first:
- PHP MySQL Introduction
- PHP MySQLi Connection
- PHP PDO Introduction
- PHP CRUD Operations
- PHP Prepared Statements
- PHP Stored Procedures
- PHP Transactions
- PHP Error Handling in DB
- PHP MySQL Connect
- PHP MySQL Create DB
- PHP MySQL Create Table
- PHP MySQL Insert Data
- PHP MySQL Get Last ID
- PHP MySQL Insert Multiple
- PHP MySQL Prepared Statements
- PHP MySQL Select Data
- PHP MySQL Where
- PHP MySQL Order By
- PHP MySQL Delete Data
- PHP MySQL Update Data
- PHP MySQL Limit Data