Subjects

All subjects Django Java Python React Spring Boot JavaScript PHP
Sign Up Free
Interview question

How do you prevent SQL injection in PHP database queries? PHP database queries में SQL injection कैसे रोकें?

Answer

SQL injection happens when untrusted input is concatenated directly into a SQL query, letting attackers alter query logic.

Vulnerable:
$sql = "SELECT * FROM users WHERE email = '$email'";
An input like ' OR '1'='1 bypasses the WHERE clause entirely.

Safe:
$stmt = $pdo->prepare('SELECT * FROM users WHERE email = ?');
$stmt->execute([$email]);
PracticeWhy it helps
Prepared statementsSeparates code from data
Input validationRejects malformed input early
Least privilege DB userLimits damage if breached

SQL injection तब होता है जब untrusted input सीधे SQL query में जोड़ दिया जाता है।

असुरक्षित:
$sql = "SELECT * FROM users WHERE email = '$email'";

सुरक्षित:
$stmt = $pdo->prepare('SELECT * FROM users WHERE email = ?');
$stmt->execute([$email]);

Was this answer clear?