View Full Version : Mysql query from array values

01-20-2012, 02:37 PM
Hello Guys,

I have a question

I have the following query

$result3 = mysql_query("SELECT * FROM table1 WHERE ad_id='$id2'")
or die(mysql_error());

$row3 = mysql_fetch_array( $result3 );

// Grab all the var
$features = $row3['features'];

I am turning it into an array (comma separated)

$feature2 = explode(",", $features);

The result is like so

Array ( [0] => 5 [1] => 9 [2] => 13

I want to query the features for just the ids (F_name) are for the features . This query will show all.. I would like to just display the f_name values from the array query.

// build and execute the query
$sql = "SELECT * FROM features";
$result = mysql_query($sql);

// iterate through the results
while ($row = mysql_fetch_array($result))
echo $row['f_name'];
echo "<br />";

Please Advise..

Thanks, Dan

01-20-2012, 03:20 PM
Are you trying to search for something involving more than one table?
I'm wondering why the last script you posted doesn't work.

01-20-2012, 06:04 PM
Hi Mesiem,

It doesn't work because of this

$sql = "SELECT * FROM features";

There should be a WHERE clause in there..

I am querying from one table

and grabbing the F_name (from the id) from another table..

01-20-2012, 08:13 PM
oh ... what you're doing will involve JOIN, JOIN LEFT, or something like that ...

See this:

I'm not so much of an expert in multiple tables.
I suppose I should learn it too.


01-21-2012, 08:22 AM
maybe mysql_fetch_assoc instead of mysql_fetch_array?

01-23-2012, 01:06 PM
FYI - This is what I used to fix my issue

$feature2 = explode(",", $features);

$feature3 = implode(',', $feature2);

// First Query to grab Feature1
$result = mysql_query("SELECT * FROM features WHERE f_id IN ( $feature3 )
") or die("Sql error : " . mysql_error());

while($row = mysql_fetch_assoc ($result)){
echo "<br>".$f_name;

01-23-2012, 01:17 PM
features has a many to many relationship with table1. You should create a third flattening table instead of a comma separated field to eliminate anomalies that will occur with queries. The bonus is that it will make queries a lot easier as well.