Forum Moderators: coopster

Message Too Old, No Replies

MySQL Query foreach not working

         

maccas

12:36 pm on Oct 5, 2005 (gmt 0)

10+ Year Member



Hi, anyone point me in the right direction as to why the below doesn't work.

$result = mysql_query("SELECT thumbnail FROM pp_categories WHERE description='$town' AND parent = '537'") or die(mysql_error());
while($row = mysql_fetch_array( $result )) {
$thumbnail=$row['thumbnail'];
$thumbArray = array($thumbnail);
foreach($thumbArray as $thumbid) {
$result = mysql_query("SELECT id, cat, bigimage FROM pp_photos WHERE id='$thumbid'") or die(mysql_error());
while ($row= mysql_fetch_array($result)) {
do stuff...

If I change
$thumbArray = array($thumbnail);
to
$thumbArray = array(100,200);
it works fine

and

$thumbArray = array($thumbnail);
foreach($thumbArray as $thumbid) {
print $thumbid;

prints out 100,200

arran

12:51 pm on Oct 5, 2005 (gmt 0)

10+ Year Member



$thumbnail=$row['thumbnail']; 
$thumbArray = array($thumbnail);
foreach($thumbArray as $thumbid) {

Maybe i'm missing the point but your foreach loop is only even going to have one iteration as you are reinitialising the array every time (with only one element).

Have you forgotten to close the while loop after

$thumbArray = array($thumbnail);
?

arran.

maccas

1:49 pm on Oct 5, 2005 (gmt 0)

10+ Year Member



Thanks arran, any pointers on what I am doing wrong though, how to do it for all elements?

arran

2:07 pm on Oct 5, 2005 (gmt 0)

10+ Year Member



I could be misunderstanding the requirements but i don't believe you need to use an array or a foreach loop. Also, if pp_photos.id is a primary key, you should be able to combine both queries into one.

Try something like:


$result = mysql_query("SELECT pp_photos.id, pp_photos.cat, pp_photos.bigimage FROM pp_photos, pp_categories WHERE pp_categories.thumbnail = pp_photos.id and pp_categories.description='$town' AND pp_categories.parent = '537'") or die(mysql_error());
while($row = mysql_fetch_array( $result )) {
// do something
}

maccas

2:20 pm on Oct 5, 2005 (gmt 0)

10+ Year Member



Excellent thanks so much arran!