Jump to content

Recommended Posts

Ok so I am pretty good with mysql and performing queries to get the data that I need. However, I have a question for any of you gurus out there that may be able to help me with an issue that I always run into with certain types of queries. Say I have 2 tables like this:

 

Galleries

id

name

1

My Photos

 

Gallery Photos

id

gallery_id

photo

1

1

photo1.jpg

2

1

photo2.jpg

3

1

photo3.jpg

 

Now usually when I pull this info from the database I have to do 2 separate queries in order to get the data and then link them so I would do something like.

 

<?php
//make the gallery query
$query = "SELECT * FROM galleries";
$results = mysql_query($query);

//setup an array for the galleries data
$gallery_data = array();

//loop through the galleries
for($i=0;$i<mysql_num_rows($results);$i++){
//add the gallery info to the gallery data array
$gallery_data[$i] = mysql_fetch_array($results);

//make the photos query with the gallery id
$photo_query = "SELECT * FROM gallery_photos WHERE gallery_id='".mysql_real_escape_string($gallery_data[$i]['id'])."'";
$photo_results = mysql_query($photo_query);

//setup the photos array
$photo_data = array();

//put the photos in the array
for($n=0;$n<mysql_num_rows($photo_results);$n++){
	$photo_data[$n] = mysql_fetch_array($photo_results);
}

//add the photos to the gallery array
$gallery_data[$i]['photos'] = $photo_data;
}

//now to display it is like this
if(is_array($gallery_data)){
foreach($gallery_data as $gallery){
	echo $gallery['name'];

	//show the photos
	if(is_array($gallery['photos'])){
		foreach($gallery['photos'] as $photo){
			echo $photo['photo'];
		}
	}
}
}
?>

 

So my question is there a way to get all of this data at one time. I know how to do multiple queries in one and to do joins but they can only return one row as far as I know of. The only other way that I know how to do this is by ordering them by name and selecting them directly from the photos table and then just getting the gallery name like this:

 

SELECT *,(SELECT name FROM galleries WHERE id=gallery_photos.id) AS gallery_name FROM gallery_photos ORDER BY gallery_id

 

Then I could just do one loop and see if the name has changed and if so to output the new name. But I would like to know if there is a way to get a second set of results as an array from a query so I could just select the galleries and photos all in one query. Any help is appreciated.

Link to comment
https://forums.phpfreaks.com/topic/210753-multiple-rows-query-and-displaying-data/
Share on other sites

I know how to use joins and I have used them before but it is the same issue with having to go through the gallery names and checking if it is different and then if it is outputting the next gallery name. I was more or less looking for a solution that would select the each of the galleries and create and array under it called photos or whatever and have all the photo information under it. This creates a row for each of the photos where I just want one for each of the galleries. Is it not possible?

The question was pretty much can a sql query have an sql query inside of it that returns a second set of results with more than one item, kinda like the example I provided. I have no problem with the logic and have been doing it this way for years but was just wondering if there was a more efficient or better way to do it, because sometimes the queries that I have to make have 3 or more tables linked to them and have to go through 3 different sets of results.

If your referring to this

SELECT *,(SELECT name FROM galleries WHERE id=gallery_photos.id) AS gallery_name FROM gallery_photos ORDER BY gallery_id

then it will probably work.. have you tried it ?

 

however i would use a Join

No I know that would work...That is the same thing as your join pretty much. I was talking about querying just galleries table and then inside of that query getting the photos as its own array returned via mysql, usually it will only let me return one result from a query withing a query. Just wondering if there is a way to return all the result from within another result.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.