Jump to content

SELECT - usings arrays


artworthy

Recommended Posts

I'm trying to filter a table to SELECT the values to certain rows based on condition. The first rather complex SELECT works fine yielding me a group of id's. But I want to use this id  group ($art_id) in an array for the 2nd  SELECT to get rows more straight forwardly. Hope someone ha suggestions. Thanks, Allen.

 

**the first SELECT is not included here -- I do get values with print $art_id; ...but only the last value shows up in my  image list SELECT because I need to use an array properly**

 

$QUERY1="SELECT.....etc,...  ///this works fine

 

$res = mysql_query($QUERY1);

$num = mysql_num_rows($res);

if($num>0){

while($row = mysql_fetch_array($res)){

$art_id = $row['art_id'];

 

print $art_id;

$a1 = array($art_id); ///this $a1 var didn't work in the SELECT below.

}

}

$QUERY2="SELECT * FROM artWork WHERE art_id = '$art_id'";  ///here is where I need to have an array var instead of just $art_id

 

    $res = mysql_query($QUERY2);

$num = mysql_num_rows($res);

if($num>0){

 

while($row = mysql_fetch_array($res)){

$art_title = $row['art_title'];

$artist_name = $row['artist_name'];

$art_id = $row['art_id'];

                $media = $row['media'];

 

  echo.....etc,...../// only one image (the last, of course) shows up here

Link to comment
https://forums.phpfreaks.com/topic/215982-select-usings-arrays/
Share on other sites

This code is replacing the $art_id value each time through the loop.  So, you are only collecting the last value.

while($row = mysql_fetch_array($res)){
       $art_id = $row['art_id'];
}

Collect the values in an array

while($row = mysql_fetch_array($res)){
       $art_id[] = $row['art_id'];
}

 

Of course, then this code is not going to work, since $art_id will be an array:

$QUERY2="SELECT * FROM artWork WHERE art_id = '$art_id'";

You will have to use IN and implode() the array

$QUERY2="SELECT * FROM artWork WHERE art_id IN (" . implode(',',$art_id) . ")";

This assumes that art_id is a numeric column. If it is a character (or varchar) you will have to provide quotes:

$QUERY2="SELECT * FROM artWork WHERE art_id IN ('" . implode("','",$art_id) . "')";

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.