SELECT ... FROM ... WHERE ... = ... returns empty array after fetchAll()

时间:2017-01-30 14:22:27

标签: php arrays apache pdo

I am trying to fetchAll() an array for rows in my database that are from a certain category.

<?php 
require 'database.php'; 
$class = $_GET["class"];
$classes = $conn -> prepare("SELECT * FROM items WHERE class = $class");
$classes -> execute();
$all_items_from_class = $classes -> fetchAll();
?> 

On the previous page I want the user to click on a category and lead him to a page where it loads all items of that specific category, the URL will show something like website.com/category.php?class=bla and display all items from that specific class. After trying different foreach loops and not getting any results, I checked with a var_dump() to see what is wrong with the array $all_times_from_class and to my surprise it returns a completely empty array.

When I remove the WHERE from SELECT it returns the entire array of all items, but for some reason it does not with WHERE. I used $_GET in the same way in another .php file and it works fine there, but not when I try to $_GET the class. Why does it not return an array for that specific category? Does it not recognise what class to get from the URL, does it not work for VARCHARs in the database? I'm at a loss at the moment. The only fix I know would be to foreach() the entire database and run every row through an if.

0 个答案:

没有答案