SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Zealot
    Join Date
    Nov 2005
    Posts
    171
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    query - where clause taking multiple values

    Hi all,
    just wondering if the following is possible.

    At present, I have a php array ($favourites[$y]), which holds the IDs of the records required from a table in a database.

    In order to display the result of the query, I have to to a seperate query from each value within the array. for example:

    while ( $y < $x ) {
    $y ++;
    $WHERE = "WHERE n.id =" .$favourites[$y];
    $sql = $SELECT.$FROM.$WHERE;
    $result = mysql_query($sql);
    }

    where $x is the number of values in the array.

    Just wondering if there is any way of just having 1 sql query, inseat of 1 for each value in the array. i.e. the WHERE clause in the query w2ould hold all the values held in the array.

    I want to be ableto do this because, I want to be able to order the query be different attributes and this cannot be done be producing individual queries.

    Cheers

  2. #2
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,347
    Mentioned
    63 Post(s)
    Tagged
    3 Thread(s)
    the syntax would be --

    ... WHERE n.id IN ( 1, 4, 9, 37 )

    i'll leave it up to you to generate this with php
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  3. #3
    reads the ********* Crier silver trophybronze trophy longneck's Avatar
    Join Date
    Feb 2004
    Location
    Tampa, FL (US)
    Posts
    9,854
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by dazi_behappy
    $sql = $SELECT.$FROM.$WHERE;
    where are you getting this syntax from? i've noticed a few people using this format all of a sudden.


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
  •