phadam phadam - 1 year ago 70
MySQL Question

Why is the text ignored by Mysql?

I have a problem with my php code! The problem lies in $_GET['id']. As you can see, I output the resultset of a user that matched with the user id.

The Question is, why do I get the same result (resultset of user with id 1) when I enter two different values on the url?


Shouldn't the text(randomtext) be taken in consideration, because the code seems to ignore it.


try {
$db = new PDO('mysql:host=;dbname=ptp', 'root', 'root');
} catch (PDOException $e) {

$id = $_GET['id'];
$sql = "SELECT * FROM users WHERE id = :id";

echo $id . '<br/>';

$query = $db->prepare($sql);

$query->bindParam(':id', $id);

while($r = $query->fetch(PDO::FETCH_ASSOC)) {


Answer Source

Just like PHP, MySQL will cast the string "1random" to int 1. So if your id field is a number, the query will be seen as "SELECT * FROM users WHERE id = 1" even if there is some random text after the 1

just try this in PHP : var_dump('1dfg' == 1) it will return true. it's the same in MySQL.

However, if you try to find a user with id "dfg1", nothing will be returned because this string can't be casted as an int.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download