Problem joinning tables

These are the tables I have:

Table:  unit

id  | title | description | county | space | town | active | feature

1       Test #1      Test Description     New Jersey    Apartment   Hoboken    1   1

Table: bookings

id | unitid | amount

1      1      150.00

Table: images

id | unitid | images  

1     1      001.jpg

Table: dropdown_lists

id | group_name | value
1    usa_states     Alaska
...
51 unit_type    Apartment

I have an issue in trying to make this inner join work. I would like to display 5 random listings on a page but I'm getting a blank page instead. The `unit` table is the main table with the `id` that goes to the `bookings`, `images`, table under the column `unitid`This is how I relate the rest of the tables with.  This is my code thus far:

$active = '1';
$feature = '1';
$county = 'Alaska':

$q_arr = $db->singleQuery("SELECT id, space, county, town, active, feature AS place
								  (SELECT images FROM images LIMIT 1) AS main_image,
								  (SELECT amount FROM bookings) AS unit_amount,
								  (SELECT value FROM dropdown_lists WHERE unit.county AND group_name = 'usa_states' LIMIT 1) AS state
								  (SELECT value FROM dropdown_lists WHERE unit.space AND group_name = 'unit_type' LIMIT 1) AS space 
						   FROM unit
						   INNER JOIN bookings
						   ON unit.id = bookings.unitid
						   INNER JOIN images
						   ON unit.id = images.unitid
						   WHERE active = ?int AND feature = ?int AND county = ?html
						   ORDER BY RAND() LIMIT 5", $active,$feature,$county);
?>

<?php

	foreach ($q_arr AS $k => $v)
		
	{	
		?>

				<?php
				if (!empty($v['main_image']))
				{
					?>
					<a href="./listing.php?id=<?php echo $v['id']; ?>"><img src="https://mydomain.com/user/images/classified/tn/<?php echo $v['main_image']; ?>" width="75"></a>
					<?php		
				}
				else 
				{
					?>
					<a href="./listing.php?id=<?php echo $v['id']; ?>"><img src="images/no-image.png" width="75"></a>
					<?php			
				}
					?>
                    <a href="./listing.php?id=<?php echo $v['id']; ?>"><?php echo $v['title']; ?></a><br />
                    <a href="./listing.php?id=<?php echo $v['id']; ?>"><?php echo $v['space']; ?></a><br />
                    <a href="./listing.php?id=<?php echo $v['id']; ?>"><?php echo $v['town']; ?></a><br />
                    <a href="./listing.php?id=<?php echo $v['id']; ?>"><?php echo $v['unit_amount']; ?></a><br />
                <?php
				
					}
					
						
						
					?>

Open in new window


What am I doing wrong?

Thanks
Genesis5150Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mark BullockQA Engineer IIICommented:
One problem is that the id column in the SELECT clause is ambiguous. Specify unit.id

Another is the subqueries in the SELECT clause. I'm not sure what you want to return here.

This is a query which returns a row. You can test it using SQL Fiddle http://sqlfiddle.com/#!9/2c95b/1
SELECT unit.id, space, county, town, active, feature AS place,
	images AS main_image,
	amount AS unit_amount
FROM unit
INNER JOIN bookings ON unit.id = bookings.unitid
INNER JOIN images ON unit.id = images.unitid
WHERE active = 1 AND feature = 1 AND county = 'New Jersey'
ORDER BY RAND() LIMIT 5

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.