Hudzilla.org - the homepage of Paul Hudson
Contents > Databases > Using MySQL with PHP Wish List | Report Bug | About Me ]

9.4.6     Results within results

This is NOT the latest copy of this book; click here for the latest version.

As you have seen it is remarkably easy to alter your MySQL queries using PHP variables, and it is also easy to read values from MySQL into PHP variables. The two can be combined together so that you can run a query, then use values from its result in new queries - take a look at this following code:

<?php
    mysql_connect
("localhost", "phpuser", "alm65z");
    
mysql_select_db("phpdb");
    
$result = mysql_query("SELECT ID, Name FROM conferences;");
    
    while (
$row = mysql_fetch_assoc($result)) {
        
extract($row, EXTR_PREFIX_ALL, "conf");
        print
"<B>Speakers at $conf_Name:</B><BR />";
        
$subresult = mysql_query("SELECT ID, Name FROM confspeakers WHERE Conference = $conf_ID;");

        while (
$subrow = mysql_fetch_assoc($subresult)) {
            
extract($subrow, EXTR_PREFIX_ALL, "speaker");
            print
"&nbsp;&nbsp;&nbsp;&nbsp;$speaker_Name<BR />";
        }

        print
"<BR />";
    }
?>

That code pulls out all conferences from a table called "conferences", extracts each conference ID, then looks up a list of speakers who were at that conference. To try the code out you will need to create the tables using the following schema, then enter your own data:

CREATE TABLE conferences (ID INT AUTO_INCREMENT PRIMARY KEY, Name CHAR(100));
CREATE TABLE confspeakers (ID INT AUTO_INCREMENT PRIMARY KEY, Name CHAR(100), Conference INT);

Once you create the tables and add some conference and speakers, you should see something like this when viewed through your web browser:







<< 9.4.5 Mixing in PHP variables: mysql_escape_string()   9.4.7 Advanced formatting >>
Table of Contents
Want to see this stuff in print? PHP in a Nutshell takes the core topics covered here, adds in thousands of edits from the editorial team and myself, and combines them to make an unbeatable reference for PHP programmers at all levels.



My latest book has hundreds more tips on how to use PHP, Apache, and MySQL, plus Perl, Python, shell scripts, performance tuning, and more!



Top-right shadow
 
Bottom-left shadow Bottom shadow

Comments from other readers
Steve A. - 13 Oct 2008

FWIW my post below was on June 7, 2006 since the dates aren't working correctly.

Steve A. - 13 Oct 2008

Michael B. How about this:
<?php
//Connect to server, select database
$result = mysql_query("select confspeakers.name as Speakers, conferences.name as Conference from confspeakers, conferences where conference = conferences.id;");

$confyear="";
while ($row = mysql_fetch_assoc($result)) {
extract($row);

if($confyear != $Conference) {
$confyear = $Conference;
print "<B>Speakers at $Conference:</B><br/>";
}

print " &nbsp;&nbsp;&nbsp;&nbsp;$Speakers<BR />";


}

?>
In the query, all the data is retrieved from both tables with one request. Then, using only one loop, an if statement is set up to print the conference year ($Conference, obtained from extract($row)) ONLY if it has changed since the last iteration of the loop. Then it prints ALL the speakers for that conference, and starts the loop again.

Also, I threw in "as Speakers" and "as Conference" in the query so it didn't return "name" and "name" which would be a bit confusing.

Michael Baumgarten - 13 Oct 2008

I was wondering if this method is inefficient once many users begin accessing the page.

The reason I'm wondering is because effectively, You are doing 5 queries on your example...

Is there additional methods to getting the same result, and limiting it to only 1 query?

I'm not sure if this method will break over increased bandwidth, but I haven't found much on the web regarding this method.



Add comment
Please note that by posting a comment here you are committing it to the public domain. This is important so that others can make use of your code themselves, and also so that I can incorporate helpful notes directly into the main text. Comments are limited to 2000 characters in length.

If you are reporting an error in the content, please tell me directly.

Your name/email address:
Your comment:
 
Now, in order to verify that you're a real person, please answer this simple question: what is five plus three?
The answer is:
(please write in
numbers, eg 19)


Top-right shadow
 
Bottom-left shadow Bottom shadow