Jillian Koskie wrote: > Okay... I'm missing something obvious. > > I've got a PHP query, and based on the results of a query, I need to do > another query for the image name. This would be very easy in ColdFusion > and so I'm wondering how to do it in PHP. > > My code: > > <?php > $dbh=mysql_connect ("localhost", "database", "password") or die ('I > cannot connect to the database because: ' . mysql_error()); > mysql_select_db ("my_database"); > > // select the table > $result = mysql_query("select * from Rentals, pics WHERE featured = 1 > AND ORDER BY RAND() LIMIT 1");
This is a Cartesian product that is subsequently LIMITed. Is that intentional? > // format results by row > while ($row = mysql_fetch_array($result)) { > $id = $row["ID"]; > $pic = mysql_query("select img_name from pics WHERE rntID = 31"); I don't fully understand the logic, but can't you just grab the exact data you need in one query? SELECT rnt.ID, pics.img_name FROM Rentals rnt INNER JOIN pics ON rnt.ID = pics.rntID WHERE featured = 1 ORDER BY RAND() LIMIT 1 Jochem ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~| Logware (www.logware.us): a new and convenient web-based time tracking application. Start tracking and documenting hours spent on a project or with a client with Logware today. Try it for free with a 15 day trial account. http://www.houseoffusion.com/banners/view.cfm?bannerid=67 Message: http://www.houseoffusion.com/lists.cfm/link=i:4:208021 Archives: http://www.houseoffusion.com/cf_lists/threads.cfm/4 Subscription: http://www.houseoffusion.com/lists.cfm/link=s:4 Unsubscribe: http://www.houseoffusion.com/cf_lists/unsubscribe.cfm?user=89.70.4 Donations & Support: http://www.houseoffusion.com/tiny.cfm/54