Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-20-2012, 02:37 PM   PM User | #1
dk4210
New Coder

 
Join Date: Nov 2002
Posts: 99
Thanks: 13
Thanked 0 Times in 0 Posts
dk4210 is an unknown quantity at this point
Mysql query from array values

Hello Guys,

I have a question

I have the following query

PHP Code:
$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)

PHP Code:
$feature2 explode(","$features);
print_r($feature2); 
)

The result is like so

PHP Code:
Array ( [0] => [1] => [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.

PHP Code:
// 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
dk4210 is offline   Reply With Quote
Old 01-20-2012, 03:20 PM   PM User | #2
mlseim
Master Coder

 
mlseim's Avatar
 
Join Date: Jun 2003
Location: Cottage Grove, Minnesota
Posts: 9,045
Thanks: 8
Thanked 1,029 Times in 1,020 Posts
mlseim has a spectacular aura aboutmlseim has a spectacular aura aboutmlseim has a spectacular aura about
Are you trying to search for something involving more than one table?
I'm wondering why the last script you posted doesn't work.

Last edited by mlseim; 01-20-2012 at 03:24 PM..
mlseim is offline   Reply With Quote
Old 01-20-2012, 06:04 PM   PM User | #3
dk4210
New Coder

 
Join Date: Nov 2002
Posts: 99
Thanks: 13
Thanked 0 Times in 0 Posts
dk4210 is an unknown quantity at this point
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..
dk4210 is offline   Reply With Quote
Old 01-20-2012, 08:13 PM   PM User | #4
mlseim
Master Coder

 
mlseim's Avatar
 
Join Date: Jun 2003
Location: Cottage Grove, Minnesota
Posts: 9,045
Thanks: 8
Thanked 1,029 Times in 1,020 Posts
mlseim has a spectacular aura aboutmlseim has a spectacular aura aboutmlseim has a spectacular aura about
oh ... what you're doing will involve JOIN, JOIN LEFT, or something like that ...

See this:
http://www.informit.com/articles/art...30875&seqNum=5

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


.
mlseim is offline   Reply With Quote
Old 01-21-2012, 08:22 AM   PM User | #5
karlos81
New to the CF scene

 
Join Date: Jan 2012
Location: Belgrade, Serbia
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
karlos81 is an unknown quantity at this point
maybe mysql_fetch_assoc instead of mysql_fetch_array?
karlos81 is offline   Reply With Quote
Old 01-23-2012, 01:06 PM   PM User | #6
dk4210
New Coder

 
Join Date: Nov 2002
Posts: 99
Thanks: 13
Thanked 0 Times in 0 Posts
dk4210 is an unknown quantity at this point
FYI - This is what I used to fix my issue

PHP Code:
$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)){
        
$f_name=$row["f_name"];
        echo 
"<br>".$f_name;
    } 
dk4210 is offline   Reply With Quote
Old 01-23-2012, 01:17 PM   PM User | #7
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,653
Thanks: 4
Thanked 2,451 Times in 2,420 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
Fou-Lu is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 03:05 AM.


Advertisement
Log in to turn off these ads.