I'm trying to pull data in a database from the ID in the URL.
Basically someone clicks on a category link. Then the category name goes into the URL. So the URL looks like this when clicking on the "action" category: http://localhost:8888/test/category.php?id=action
I want to then grab everything in the database that equals to the category in the URL - so grab all "action" items in the database which is in a column called "category"
Here is the PHP code I'm working on below. I get this error:
There was an error running the query [Unknown column 'action' in 'where clause']
PHP code:
<?php
$db = new mysqli('localhost', 'root', 'root', 'test');
if($db->connect_errno > 0){
die('Unable to connect to database [' . $db->connect_error . ']');
}
$id = mysql_real_escape_string($_GET['id']);
$sql = <<<SQL
SELECT *
FROM `games_db`
WHERE `category` = ($id)
SQL;
if(!$result = $db->query($sql)){
die('There was an error running the query [' . $db->error . ']');
}
while($row = $result->fetch_assoc()){
echo '<h2>' . $row['title'] . '</h2>' . ' ' . $row['description'] . '<br /><br />';
}
echo 'Total results: ' . $result->num_rows;
// Free result set
mysqli_free_result($result);
mysqli_close($db);
?>
"$id", not($id)so it compares against a string, otherwise it treats it as a column name. also, obligatory comment about not usingmysql_*, and although it's good to see you're escaping the parameters -- prepared statements would be better again.mysql_real_escape_string