<?php
$id = $_GET['id'];
$query = "SELECT doc
FROM docs
WHERE id = '$id';";
$doctext = mysql_query($query);
?>
I’m new to PHP/MySQL and am unsure what is going wrong. The code above is supposed to get the id from the URL, which works fine, then select the doc–which is a column of data adjacent to the id column–from the table docs which matches the given $id value.
However, instead it just returns “Resource id #11”, which makes me think that I’m mixing up the SELECT, FROM, and WHERE keywords somehow, because it’s not giving an error, just the wrong data.
Why is this happening and how can I fix it?
Your query is executing fine. Your issue is that you aren’t retrieving any data from the result. The
mysql_queryfunction doesn’t automatically retrieve data, you retrieve it in a format you desire using one of themysql_fetchfunctions like mysql_fetch_row.Note that you really shouldn’t be using the ancient MySQL api, and should be using PDO (or MySQL). Also, your code is vulnerable to SQL injection and you should use bound parameters, a feature of MySQLi and PDO.