How do you get all rows that have a certain id from database?

Discussion in 'WordPress' started by Webed4it, Nov 26, 2010.

  1. #1
    Hi, I'm new to MySQL and I need to get the filenames of pictures stored in the wordpress database. I read up that you are supposed to use the $wpdb variable to access the database in WP.

    I have the gallery id and need to get a list of all the pictures/rows in the picture table by selecting those with the same id.

    Something like this:

    $result = $wpdb->query("SELECT * FROM wp_ngg_pictures WHERE galleryid=1");
    Code (markup):
    then I want to display the pictures using a loop similar to:
    
    <table><tr>
    while ($myrow = mysql_fetch_array($result))
    {
    echo "<td>".$myrow["filename"]."</td>";
    }
    </tr></tabel>
    
    Code (markup):
    That code did not work properly, what am I doing wrong?
     
    Webed4it, Nov 26, 2010 IP
  2. daljit

    daljit Well-Known Member

    Messages:
    312
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    101
    #2
    try this
    $result = $wpdb->get_results("SELECT * FROM wp_ngg_pictures WHERE galleryid=1");
    this will return you as array as an object..
     
    daljit, Nov 26, 2010 IP
  3. Webed4it

    Webed4it Peon

    Messages:
    72
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks, I think that works but how do you access the results that are returned?

    and in the SQL query, how do you use a variable for galleryid - what is the correct syntax? galleryid=$idvariable does not work.
     
    Webed4it, Nov 26, 2010 IP