#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    64
    Rep Power
    2

    Query Multiple Rows With Same Column Value


    I don't know why I'm having so much trouble with this, but I'm just trying to query the id of every row with a specific value in a given column.

    For instance:
    Table: images

    imgID|userID
    1 | 101
    2 | 102
    3 | 101

    Let's say i wanted to query all the imgID's corresponding with userID 101, is there a way to query this without creating a PHP loop?

    PHP Code:
    $uploads mysql_fetch_array(mysql_query("SELECT `imgID` FROM `images` where userID = '$user[0]'")); 
    This snippet of code will only return a single imgID, but i need all of them.
  2. #2
  3. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    119
    Rep Power
    32
    Why would you not want to create a PHP loop? If you have multiple records being returned from a query then the only way to get them displayed is with a loop (while or foreach).

    I would suggest you need to change your query:
    PHP Code:
    $uploads mysql_query("SELECT `imgID` FROM `images` where userID = '$user[0]'");
    while (
    $img_row mysql_fetch_array($uploads)) {
    //Do something here

    Before someone else tells you, mysql functions are now deprecated (and will eventually, but way in the future I would hope, become obsolete) and you should look into using mysqli or PDO instead
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    64
    Rep Power
    2
    Originally Posted by simplypixie
    Before someone else tells you, mysql functions are now deprecated (and will eventually, but way in the future I would hope, become obsolete) and you should look into using mysqli or PDO instead
    Thanks, is the mysqli syntax all the same, just mysqli instead of mysql?
  6. #4
  7. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    119
    Rep Power
    32
    Generally yes but you need to ensure your db connection uses mysqli instead of mysql to instantiate it. If you search for it there is a lot of stuff for you to learn from.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    64
    Rep Power
    2
    Originally Posted by simplypixie
    Generally yes but you need to ensure your db connection uses mysqli instead of mysql to instantiate it. If you search for it there is a lot of stuff for you to learn from.
    Yeah I'll need to do some more research on that, thanks for the heads up.
  10. #6
  11. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,957
    Rep Power
    1046
    Hi,

    if you simply replace "mysql_..." with "mysqli_...", you haven't really gained anything.

    I think this is a misunderstanding: The mysql_ functions aren't bad just because they're obsolete. That alone wouldn't really be a problem, because they're likely to exist for many more years.

    The problem is that they're difficult to use correctly. Every single value has to be escaped with mysql_real_escape_string() before being put into the query string. Since almost nobody gets that right (you didn't either, unfortunately), people using the old functions constantly end up with massive security holes.

    The new extensions, on the other hand, support prepared statements, which are a safe and foolproof way of passing values to queries. This is why many of us promote dropping the old extension in favor of the new ones. It's not about wearing the latest fashion or something.
  12. #7
  13. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    119
    Rep Power
    32
    Thank you for elaborating as I couldn't think how to word it all before

IMN logo majestic logo threadwatch logo seochat tools logo