Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4

Thread: select ids

  1. #1
    New Coder
    Join Date
    May 2012
    Posts
    38
    Thanks
    0
    Thanked 0 Times in 0 Posts

    select ids

    $query = mysql_query("Select name From names Where id = '".$ids."' ") or die(mysql_error());

    now my $ids can be:
    1
    2
    ALL

    If $ids = ALL , then how it takes all of id-s which is 1 or 2?

  • #2
    Regular Coder
    Join Date
    May 2011
    Posts
    240
    Thanks
    1
    Thanked 56 Times in 55 Posts
    PHP Code:
    $sql "SELECT name FROM names";
    if (
    $ids != "ALL")
            
    $sql .= " WHERE id=" . (int)$ids;
    $query mysql_query($sql) or die(mysql_error()); 

  • #3
    New Coder
    Join Date
    May 2012
    Posts
    38
    Thanks
    0
    Thanked 0 Times in 0 Posts
    it doesn't work

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    You'll have to remove the datatype cast to an integer and treat it as a string. We know this since you are containing both strings and numbers in a field (which is probably not a great idea to start with), so it therefore must be a string type. MySQL can be datatype strict if its set that way.
    PHP Code:
    $sql "SELECT name FROM names";
    if (
    $ids != "ALL")
    {
            
    $sql .= " WHERE id='" mysql_real_escape_string($ids) . "'";
    }
    $query mysql_query($sql) or die(mysql_error()); 


  •  

    Posting Permissions

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