简体   繁体   中英

How to get an array of values from a mysql column based on an array

I'm new to PHP and as I'm getting into arrays its throwing me for a loop. No pun intended. Everything below is giving me what I need so far. $datum contains an array of submission ID's I need. What I'd like to do next is query a table to match all of my submission ID's in $datum to an Image ID column and put that result into an array containing all of the image ID's. Then I need to query the image table to get the file name for each of the Image ID's and put it into a SELECT box. Im just not sure how to work the loops with the arrays. I just can't make FOR EACH and WHILE do what I need. Ive put what Id like to do in plain english below the code. Thanks for any help.

<?php
$mask5 = $_GET["var1"];
require("codebase/grid_connector.php");
$res = mysql_connect("", "", "");
mysql_select_db("supplydb");
//Get Category ID
$cat    = mysql_query("SELECT category FROM submissions WHERE submissions.submission_id='$mask5'");
$rows   = mysql_fetch_array($cat, MYSQL_ASSOC);
$array  = $rows['category'];
//Get Manufactuer ID
$man    = mysql_query("SELECT manufacturer_id FROM submissions WHERE submissions.submission_id='$mask5'");
$arows  = mysql_fetch_array($man, MYSQL_ASSOC);
$array1 = $arows['manufacturer_id'];
//Get All Submission ID's for this popup
$datum  = array();
$result = mysql_query("SELECT submission_id FROM submissions WHERE submissions.category='$array' AND submissions.manufacturer_id='$array1'");
while ($rowd = mysql_fetch_array($result, MYSQL_ASSOC)) {
    $datum[] = $rowd;
} 

////// WHAT ID LIKE TO DO.......

mysql_query("SELECT image_id FROM imagsub WHERE image_id = $datum[]...

$answers = new array from above result

THEN

mysql_query("SELECT filename FROM images WHERE image_id = $answers[]

THEN

Put into a select box.

Some of this I can struggle with and try to figure out but right now I can't seem to get an array of values from one column in a table based on another array. Any help with the rest is just gravy. Thanks in advance.

Seems like you either need to join on image_id and filename in the datum queries. I would also recommend that you not use mysql_* and use PDO , or some other wrapper instead. You should at least sanitize your input, especially if it's acquired with _GET .

It might be simpler if you create a query that joins all the data between your tables instead doing on PHP side. It's hard to understand your database and tables relationships, but definitely do it on SQL side, and don't query the database each time for each table you want to join.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM