SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Addict
    Join Date
    Jul 2013
    Location
    South of the equator, then turn left
    Posts
    361
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Help needed with query

    Hi everyone,


    for “some” reason the following isn't working. I have one query which loops out specific numbers(IDs). So there might be 5, 18, 33 ….

    I then need to print out the data for each of these Ids, using WHERE id_number = //insert specific number here. What would be the easiest way to do this? Should I first store the numbers in an array?



    Thank you very much.

  2. #2
    SitePoint Guru
    Join Date
    Nov 2003
    Location
    Huntsville AL
    Posts
    689
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $ids = array(5,18,33);
    $ids_comma_separated implode(',',$ids);

    $where sprintf("WHERE id_number IN (%s)",$ids_comma_separated);

    echo 
    $where "\n"
    Note the use of the sql IN parameter which takes a comma separated list of ids.

    You need at least one id or the sql parser will generate an error.

    Finally, there is no escaping/cleaning going on here. The assumption is that you are generating the list of ids internally so there is no possibility of a sql injection attack.

  3. #3
    SitePoint Addict
    Join Date
    Jul 2013
    Location
    South of the equator, then turn left
    Posts
    361
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi there ahundiak,

    thanks for the help, I appreciate it.


    Would you know how I can add the results of the first query to an array? In other words the while loop will return all the numbers which are then assigned to an array.


    $r = @mysqli_query($dbc, $q);
    while($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {
    // fetch all IDs here and assign to an array
    }

    Thank you!

  4. #4
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,169
    Mentioned
    153 Post(s)
    Tagged
    0 Thread(s)
    Yep, just define the array, then add the entry to it.

    PHP Code:
    $r = @mysqli_query($dbc$q); 
    $arrayOfIds = array();
    while(
    $row mysqli_fetch_array($rMYSQLI_ASSOC)) {
    // fetch all IDs here and assign to an array
    $arrayOfIds[] = $row['ID'];


  5. #5
    SitePoint Addict
    Join Date
    Jul 2013
    Location
    South of the equator, then turn left
    Posts
    361
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Problem solved!

    Thank you cpradio.

  6. #6
    SitePoint Guru bronze trophy
    Join Date
    Dec 2003
    Location
    Poland
    Posts
    930
    Mentioned
    7 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by cpradio View Post
    PHP Code:
    while($row mysqli_fetch_array($rMYSQLI_ASSOC)) { 
    and a shorter version:
    PHP Code:
    while($row mysqli_fetch_assoc($r)) { 


Bookmarks

Posting Permissions

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