SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Enthusiast
    Join Date
    Jun 2005
    Posts
    40
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    MySQL Query Help

    Hi, is it possible to have a query somthing like this:

    select * from members where 'this_row' is less than $this_variable and more than $that_variable and 'this_other_row' is lass than $this_other_variable and more than $that_other_variable

    Can somebody please turn this into a real query, or if thats impossible, can you join it up with PHP and so i have somthing that returns the same result.

    Cheers,

    Sean Mascali

  2. #2
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $sql "SELECT * FROM members WHERE col1 < $this_variable AND col2 < $that_variable AND (col3 < $this_other_variable AND col3 > $that_other_variable)";

    // 

  3. #3
    SitePoint Enthusiast
    Join Date
    Jun 2005
    Posts
    40
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    heres what i have done

    Thanks,

    I tried to change it a bit and its not working properly, can you help me correct this please.

    $query = "SELECT * FROM members WHERE trade LIKE $trimmed AND (user_pc_lat < $maxlat AND user_pc_lat > $minlat) AND (user_pc_lon < $maxlon AND user_pc_lon > $minlon) order by trade";

    Thanks,

    Sean

  4. #4
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Use single quotes around $trimmed since it's a string type column (as opposed to numeric):
    PHP Code:
    $query "SELECT * FROM members WHERE trade LIKE '$trimmed' AND (user_pc_lat < $maxlat AND user_pc_lat > $minlat) AND (user_pc_lon < $maxlon AND user_pc_lon > $minlon) order by trade"
    If you're trying to retrieve rows where trade is equal to $trimmed, just use =
    PHP Code:
    $query "SELECT * FROM members WHERE trade = '$trimmed' AND (user_pc_lat < $maxlat AND user_pc_lat > $minlat) AND (user_pc_lon < $maxlon AND user_pc_lon > $minlon) order by trade"
    If your intent using LIKE was to match anything with $trimmed inside the trade column, % is the wildcard character in SQL:
    PHP Code:
    $query "SELECT * FROM members WHERE trade LIKE '%$trimmed%' AND (user_pc_lat < $maxlat AND user_pc_lat > $minlat) AND (user_pc_lon < $maxlon AND user_pc_lon > $minlon) order by trade"
    To find out where the problem is when a query fails, print mysql_error():
    PHP Code:
    $result mysql_query($query$connection) or die("An error occurred executing $query: " mysql_error()); 

  5. #5
    SitePoint Enthusiast
    Join Date
    Jun 2005
    Posts
    40
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Thanks Dan

    Yes it works now, thank you.


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
  •