Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    Regular Coder
    Join Date
    Nov 2002
    Posts
    104
    Thanks
    13
    Thanked 0 Times in 0 Posts

    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

  • #2
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,502
    Thanks
    8
    Thanked 1,089 Times in 1,080 Posts
    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.

  • #3
    Regular Coder
    Join Date
    Nov 2002
    Posts
    104
    Thanks
    13
    Thanked 0 Times in 0 Posts
    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..

  • #4
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,502
    Thanks
    8
    Thanked 1,089 Times in 1,080 Posts
    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.


    .

  • #5
    New to the CF scene
    Join Date
    Jan 2012
    Location
    Belgrade, Serbia
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    maybe mysql_fetch_assoc instead of mysql_fetch_array?

  • #6
    Regular Coder
    Join Date
    Nov 2002
    Posts
    104
    Thanks
    13
    Thanked 0 Times in 0 Posts
    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;
        } 

  • #7
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •