Results 1 to 3 of 3

Thread: Pleae rectify my code

  1. #1
    Join Date
    Sep 2008
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Pleae rectify my code

    Iam trying to compare a row with array of values in php as follows

    $array[0]
    $array[1]
    .
    .
    .
    .
    .
    $array[n]

    $result=mysql_query("select * from databasetable where row1=any($array)");

    the above code is not working. Please any body solve my problem.

  2. #2
    Join Date
    Sep 2008
    Posts
    26
    Thanks
    2
    Thanked 4 Times in 4 Posts

    Default

    Are you looking for
    foreach (array_expression as $value)
    foreach (array_expression as $key => $value)

    ?

    Code:
    $stuff = array("foo", "bar", "123");
    
       //cycle through all array elements
    foreach ($stuff as $singleelement) 
    {
    
    //fetch! Good dog!
    $result.=mysql_query("select * from databasetable where row1=$singleelement"); 
    
    //  .= will put everyting in the string $result, while = would just overwrite the last query
    }
    (or something like that, check the syntax, my php is a little rusty ;-)

    the above code is not working. Please any body solve my problem.[/QUOTE]

  3. #3
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Gods no, don't do that! That will execute a query for every row in the array -- horribly inefficient.

    Rather, try:
    Code:
    function quote_mysql_string($s) {
      return '\'' . mysql_real_escape_string($s) . '\'';
    }
    
    $result = mysql_query(sprintf('SELECT * FROM databasetable WHERE row1 in (%s)', implode(', ', array_map('quote_mysql_string', $array))));
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

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
  •