SELECT with an array ()

I am trying to filter a table to select values ​​for specific rows based on a condition. The first rather complex SELECT works fine with the id group. But I want to use this id group ($ art_id) in the array for the second SELECT to get the rows more straightforward. Hope someone has suggestions. Thanks, Allen.

the first SELECT is not included here - I get the values ​​with the print $ art_id; ... but only the last value appears in my SELECT image list, because I need to use the array correctly

$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.
   }
}
///here is where I need to have an array var instead of just $art_id
$QUERY2="SELECT * FROM artWork WHERE art_id = '$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
+3
source share
3 answers

you can solve this problem using just the request:

, art_id table1, :

SELECT art_id FROM table1

:

SELECT * FROM artWork WHERE art_id = '$art_id'

, :

SELECT * FROM artWork WHERE art_id in ( SELECT art_id FROM table1 );
+3

, .

SELECT [...] WHERE `art_id` IN (SELECT `art_id` [...])

, .

PHP, IN :

$values = array(1,2,3);
'WHERE `art_id` IN ('.implode(',', $values).')'
0

Use a connection, it is quite simple and much faster.

SELECT aw.* FROM table1 t
JOIN artWork aw ON aw.art_id=t.art_id
WHERE (CONDITIONS);

where CONDITIONS are the conditions from your first request.

0
source

Source: https://habr.com/ru/post/1769829/


All Articles