Question
This query here is supposed to SELECT * pages where the $_GET['variables'] match the conditions. The $_GET['variables'] are $page_type = type of page AND $loc = location to search
. The problem is that I have it set to LIMIT 20, but it only renders one page.
function SearchByTypeLoc($page_type, $loc) {
$query = mysql_query("
SELECT
`$page_type`.title AS title,
`$page_type`.url_title AS url_title,
`$page_type`.page_type AS page_type,
`$page_type`.street AS street,
`$page_type`.city AS city,
`$page_type`.state AS state,
`$page_type`.city_state_zip AS city_state_zip,
`$page_type`.phone AS phone,
LEFT(`$page_type`.body, 100) AS body,
LEFT(`$page_type`.type, 50) AS type,
GROUP_CONCAT( i.image_loc ) AS images
FROM `$page_type`
JOIN page_images i USING( title )
WHERE
`$page_type`.page_type = '$page_type' AND
`$page_type`.city = '$loc'
ORDER BY title
LIMIT 20");
while($fetch = mysql_fetch_assoc($query)) { ...
?>
Any idea how to make it not LIMIT 1?
Solution
GROUP_CONCAT is an aggregate function,
try explicitly add a
GROUP BY `$page_type`.title
OTHER TIPS
It doesn't LIMIT 1
, but there is only $page_type
which matches the criteria. You join on title
(why not on an id?) so there must only be one $page_type
with images with a matching title
.
If you want to return pages which also may not have any images then you need to change the JOIN
to LEFT JOIN
.
The way you link pages to images is flawed. If you change the title of a page you need to remember and update the respective title in the images table. In any case this is not a proper foreign key. You should use an auto_increment id on pages.