I have a dynamic list of categories, which is retrieved from mySQL. When the user is clicking on a category, it loads images for that specific category from the DB. This is the function that does it:
function getImages($categoryID)
{
return json_encode(select("SELECT * FROM images WHERE categoryID=$categoryID"));
}
Now I also want to retrieve also the category name again from the “categories” table (to include the name of the selected category in other part of the page). I tried to do this:
function getImages($categoryID)
{
return json_encode(select(
"SELECT categories.categoryName, images.imageFileName, images.imageHeader
FROM images JOIN categories
ON categories.categoryID = images.categoryID
WHERE categoryID=$categoryID"));
}
but that didn’t work, now I don’t even get the images.
How can it be done?
In your WHERE clause, categoryID is ambiguous.
Since you have two tables with categoryID, you need to identify which one the WHERE should be looking at.
To make life easier for yourself, you might want to consider aliasing your tables, like this:
Also, which language are you developing in? If this is a web app, you might want to consider escaping $categoryID to protect against SQL injection.