← Back to MySQL Course | Chapter 18: User Management & Security | Lesson 6 of 6

MySQL Prepared Statements

एक prepared statement blank boxes वाले एक form जैसा है: question पहले fix होता है और answers बाद में डाले जाते हैं, इसलिए tricky input question नहीं बदल सकता।
Syntax
sql
PREPARE statement_name FROM 'SQL with ? placeholders';
SET @variable = value;
EXECUTE statement_name USING @variable;
DEALLOCATE PREPARE statement_name;

एक Prepared Statement क्या है?

एक prepared statement एक query को दो steps में split करता है: पहले database SQL structure को literal values के बजाय placeholder markers (?) के साथ compile करता है, फिर actual input उन placeholders को भरने के लिए अलग से भेजा जाता है। क्योंकि structure किसी data के आने से पहले lock हो जाता है, user input कभी SQL syntax की तरह reinterpret नहीं हो सकता।

उदाहरण: What Is a Prepared Statement?

sql
PREPARE stmt FROM 'SELECT * FROM users WHERE id = ?';
SET @id = 1;
EXECUTE stmt USING @id;
DEALLOCATE PREPARE stmt;

PREPARE, EXECUTE, और DEALLOCATE

raw MySQL में, PREPARE stmt_name FROM sql_with_placeholders statement को एक बार compile करता है; EXECUTE stmt_name USING @var हर placeholder से bound एक specific value के साथ इसे चलाता है; DEALLOCATE PREPARE stmt_name काम खत्म होने पर compiled statement free करता है। same compiled statement को हर बार SQL recompile किए बिना अलग-अलग values के साथ बार-बार execute किया जा सकता है।

उदाहरण: PREPARE, EXECUTE, and DEALLOCATE

sql
PREPARE get_user FROM 'SELECT * FROM users WHERE id = ?';
SET @id = 1;
EXECUTE get_user USING @id;
SET @id = 2;
EXECUTE get_user USING @id;
DEALLOCATE PREPARE get_user;

Vulnerable Query बनाम Prepared Statement

एक vulnerable query raw input को सीधे string में concatenate करके SQL बनाती है, इसलिए ' OR 1=1 -- जैसी एक value पूरी तरह query का meaning बदल सकती है। Prepared-statement version चाहे कोई भी value bound हो exact same placeholder text रखता है, क्योंकि driver value को pure data की तरह भेजता है, कभी उस SQL के हिस्से की तरह नहीं जिसे यह parse करता है।

उदाहरण: Vulnerable Query vs. Prepared Statement

sql
-- Vulnerable: concatenated input can change the query's meaning
SET @unsafe_input = "' OR 1=1 -- ";
-- Prepared statement: the value is always treated as pure data
PREPARE stmt FROM 'SELECT * FROM users WHERE username = ?';
EXECUTE stmt USING @unsafe_input;
DEALLOCATE PREPARE stmt;

कई Parameters Bind करना

एक single prepared statement में कई placeholders हो सकते हैं, और हर एक USING clause में अपने खुद के variable से bound है, query में दिखने वाले same left-to-right order में। यह prepared statements को कई WHERE conditions या multi-column INSERTs वाली realistic queries के लिए practical बनाता है, सिर्फ single-value lookups नहीं।

उदाहरण: Binding Multiple Parameters

sql
PREPARE stmt FROM 'SELECT * FROM orders WHERE customer_id = ? AND status = ?';
SET @cid = 1, @status = 'paid';
EXECUTE stmt USING @cid, @status;
DEALLOCATE PREPARE stmt;

Application Code में Prepared Statements

ज़्यादातर application database libraries (PHP में PDO, Python में mysql-connector, Java में JDBC) इस PREPARE/EXECUTE mechanism को एक simpler API के पीछे wrap करती हैं, इसलिए आप production code में शायद ही कभी हाथ से raw PREPARE statements लिखते हैं। underlying mechanism समझना अभी भी मायने रखता है, क्योंकि यह exactly explain करता है कि किसी भी language में parameterized queries पिछले topic में दिखाई गई injection technique से immune क्यों हैं।

उदाहरण: Prepared Statements in Application Code

sql
-- Raw MySQL PREPARE/EXECUTE, the mechanism PDO/JDBC/mysql-connector wrap internally:
PREPARE stmt FROM 'INSERT INTO logs (message) VALUES (?)';
SET @msg = 'user logged in';
EXECUTE stmt USING @msg;
DEALLOCATE PREPARE stmt;
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. एक ? placeholder इस्तेमाल करने के बजाय string को value अंदर पहले से रखकर बनाना, जो अभी भी injectable है।
  2. USING @var से value pass करना भूल जाना, या गलत संख्या में values pass करना।
  3. DEALLOCATE PREPARE भूल जाना, ताकि statement session के लिए memory में रहे।
चैप्टर सारांश
  • CREATE USER और DROP USER accounts add और remove करते हैं, जबकि GRANT और REVOKE privileges manage करते हैं।
  • SQL injection एक security risk है जहाँ untrusted input एक query बदल देता है।
  • Prepared statements SQL injection के against protect करने में मदद करते हैं।
🔒

Chapter Quiz — Complete all 6 topics to unlock

0/6 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.