$id=$_GET['id']// reading from URL the id value i.e 2489
We can build a query using this variable like this.
$sql="SELECT * FROM student where id=$id";
We can send the query string like this
<a href='http://www.sitename.com?id=10; DROP TABLE student--&user=tom'>Link</a>
While receiving will get the data ( with variable ) like this.
$id='10; DROP TABLE student--';
The second part of the variable data saying DROP TABLE student--
will be executed as one more query and delete the student table. require "config.php"; // Database Connection
$id=$_GET['id'];
$id='10; DROP TABLE student--';// Injected data for id
$sql="SELECT * FROM student where id=$id";
$step=$dbo->query($sql);
$row = $step->fetch(PDO::FETCH_OBJ);
echo "<hr><br>Admin = $row->id";
echo "<br> name =$row->name<br>";
echo "<hr>";
The attacker manipulates the query with ' OR '1'='1, which always evaluates to true
$name = $_GET['name']; // User input from URL or form
$query = "SELECT * FROM users WHERE name = '$name'";
// If the input is: ' OR '1'='1
// The query becomes: SELECT * FROM users WHERE name = '' OR '1'='1'
// This returns all records, bypassing authentication
$sql="SELECT * FROM student WHERE id=:id";
$count=$dbo->prepare($sql);
$count->bindParam(":id",$id,PDO::PARAM_INT,3);
if($count->execute()){
echo " Success <br>";
$row = $count->fetch(PDO::FETCH_OBJ);
print_r($row);
echo "<hr><br>Admin = $row->userid";
echo "<br> pw =$row->password<br>";
SELECT * FROM student WHERE Id = 105 OR 1=1;
Here we will expose all the columns with all the rows as 1=1
is always True.
<?php
$userid='"" or ""=""';
$password='"" or ""=""';
echo "SELECT * FROM my_table WHERE userid=$userid AND password=$password";
//Output//
//SELECT * FROM my_table WHERE userid="" or ""="" AND password="" or ""=""//
?>
Download Zip file to test your PHP PDO script
PDO ReferencesCollecting single record from table
Author
🎥 Join me live on YouTubePassionate about coding and teaching, I publish practical tutorials on PHP, Python, JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and project‑oriented with real examples and source code.