Re: Stupid newbie looping MySQL question
| From: | Rasmus Lerdorf | Date: | Sun, 07 Jan 2001 22:50:43 +0000 |
| Subject: | Re: Stupid newbie looping MySQL question | ||
| References: | 1 | Groups: | php.db |
| Request: | Send a blank email to php-db+get-5688@lists.php.net to get a copy of this message | ||
> What I want to do:
> 1) First, pull a list of topics from one table;
>
> 2) Then, use each topic one by one to pull info from *another* table;
>
> 3) Once I have that info, process *it* piece by piece.
>
> I've tried a few things, and I've looked at my books and the manual,
> but PHP keeps telling me the supplied argument is not a valid MySQL
> resource, and I confess that I'm not entirely sure what I'm doing yet.
> :) Thanks in advance.
$result1 = mysql_query("select topic from table1");
while($row1 = mysql_fetch_row($result1)) {
$result2 = mysql_query("select * from table2 where
field='".$row1[0]."'");
$info[$row1[0]] = mysql_fetch_row($result2);
mysql_free_result($result2); // important
}
At this point you now have an array that looks something like:
$info['apples'] = "Delicious and juicy";
$info['cars'] = "blah blah";
You can walk through such an array using something like this:
foreach($info as $topic => $text) {
echo "$topic: $text<br>\n";
}
This would display:
apples: Delicious and juicy
cars: blah blah
If you are using PHP 3 then replace the foreach() line with:
while(list($topic,$text)=each($info))
-Rasmus