haydenbarton96 haydenbarton96 - 2 years ago 90
SQL Question

Can't seem to get $_GET['id'] to work

I'm trying to get data from a database but cant seam to get the following working, does anyone know why?

I have tried

too and shows nothing, the db connection and everything is working too.

$id = $_GET['id'];
$query = $db->prepare('SELECT id, title, content FROM articles WHERE id = $id');
$article = $query->fetchall();

This does however
the id from the URL correctly.


Any help is appreciated!

Answer Source

Important note:

Variables inside single qoutes are not interpolated. They're just literal strings

Much better to bind them properly:

$id = $_GET['id'];
$query = $db->prepare('SELECT id, title, content FROM articles WHERE id = :id');
$query->bindParam(':id', $id);
$article = $query->fetchAll(PDO::FETCH_ASSOC);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download