Page is a not externally linkable
rlange - 8:28 pm on Jun 11, 2012 (gmt 0)
Davidkarate wrote:
I got everything working with this query, except the result is repeating user information for every image.
Yeah, that's one of the things about SQL; it doesn't return complex objects. It just returns rows.
In your situation, I would probably do two separate queries. One to grab the information about the classified itself (title, description, state, city) and a second one just to grab the images associated with that classified.
<?php
$result = mysqli_query( "SELECT `classifieds`.`title`, `classifieds`.`description`, `state`.`statename`, `city`.`city`
FROM `classifieds`
LEFT JOIN `state` ON `classifieds`.`state_id` = `state`.`id`
LEFT JOIN `city` ON `classified`.`city_id` = `city`.`id`
WHERE `classifieds`.`id` = " . $id . "
AND `authorized` = '1'" );
$classified = mysqli_fetch_assoc( $result );
$result = mysqli_query( "SELECT `image_path` FROM `img` WHERE `classified_id` = " . $id );
$classified['images'] = array();
while( false !== ( $row = mysqli_fetch_assoc( $result ) ) {
$classified['images'][] = $row;
}
var_dump( $classified );
?>
Obligatory Warnings:
$id is properly sanitized or you open yourself up to SQL injection attacks. This can't be stressed enough.