SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Enthusiast
    Join Date
    Aug 2004
    Location
    India
    Posts
    94
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    in_array : problem while using in query

    I have an array $IdArray which stores the id 's that have been visited. I want to get id 's which haven't been shown till now....... for this I am using in_array

    but it's not working inside query ...

    Code:
    $sql = $db_object->query("Select id from table_name where (in_array(id,$IdArray) == 0) order by id desc");
    if (DB::isError($sql))
    {
         die($sql->getMessage());
    }
    this page is showing DB Error: syntax error

    but when I try to independently check in_array it is working ...

    Code:
    if(in_array($id,$IdArray) == 0)
    {
    	echo("new");
    }
    else
    {
    	echo("old");
    }

  2. #2
    That's Right. notepad_coder's Avatar
    Join Date
    Apr 2002
    Location
    Colorado
    Posts
    835
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    in_array() returns either TRUE or FALSE. So your query would either be:
    Code:
    SELECT id FROM table_name WHERE TRUE ORDER BY id DESC
    
    or
    
    SELECT id FROM table_name WHERE FALSE ORDER BY id DESC
    which wouldn't work.

    Now if you did something like this..
    PHP Code:
    if (in_array($id$IdArray) == 0) {
        
    $visited "no";
    } else {
        
    $visited "yes";
    }
    // change $visited to fit your table.
    $sql $db_object->query("SELECT id FROM table_name WHERE visited = '".$visited."' ORDER BY id DESC");
    if (
    DB::isError($sql) {
        die(
    $sql->getMessage());

    - the lid is off the maple syrup again!

  3. #3
    SitePoint Enthusiast
    Join Date
    Aug 2004
    Location
    India
    Posts
    94
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by notepad_coder View Post
    in_array() returns either TRUE or FALSE. So your query would either be:
    Code:
    SELECT id FROM table_name WHERE TRUE ORDER BY id DESC
    
    or
    
    SELECT id FROM table_name WHERE FALSE ORDER BY id DESC
    which wouldn't work.
    this works ......

    i think u haven't got my question...

    i wanted the list of id which are not there in $IdArray ... so i wanted to use in_array in query

  4. #4
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,014
    Mentioned
    187 Post(s)
    Tagged
    2 Thread(s)

    query

    Quote Originally Posted by kaushik_acharya
    is working ...
    if(in_array($id,$IdArray) == 0)
    Quote Originally Posted by kaushik_acharya
    not working inside query ...
    "Select id from table_name where (in_array(id,$IdArray) == 0) order by id desc"
    You forgot the "$" in front of the in_array id

  5. #5
    SitePoint Enthusiast
    Join Date
    Aug 2004
    Location
    India
    Posts
    94
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i think u people are getting the wrong question ......


    PHP Code:
    if(in_array($id,$IdArray) == 0)
    {
        echo(
    "new");
    }
    else
    {
        echo(
    "old");

    this i had done when i know the $id .......... this was just a test to check if function in_array is working or not .....
    for e.g in http://www.jalsaghar.com/image.php?id=66
    i know $id = 66

    but

    PHP Code:
    $sql $db_object->query("Select id from table_name where (in_array(id,$IdArray) == 0) order by id desc");
    if (
    DB::isError($sql))
    {
         die(
    $sql->getMessage());

    i want to get all the id which is not present in $IdArray

    hope i have put my question in a proper manner ..

  6. #6
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,014
    Mentioned
    187 Post(s)
    Tagged
    2 Thread(s)

    in array

    OK, I think I see what you're trying to do. Something like
    query = "Get the values from the id field in the table_name table where the value is not in the $IdArray array and order the results by descending id values"
    I think the problem is that you are trying to get values based on what you know they are after you've gotten them, it won't work this way.
    I think there are 2 ways to do this correctly.
    Preferrably, you could construct a valid query using the $IdArray. eg. something like
    PHP Code:
    $query "SELECT id FROM table_name WHERE (1 = 1) ";
    foreach (
    $IdArray as $Id) {
    $query .= "AND id != $Id ";
    }
    $query .= "ORDER BY id DESC"
    Or you could get all the results, and then use PHP code to get at the ones you're interested in.

  7. #7
    SitePoint Enthusiast
    Join Date
    Aug 2004
    Location
    India
    Posts
    94
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Mittineague,
    Lots of thanks.........

    Kaushik
    India

  8. #8
    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 ... WHERE ID NOT IN (" implode(","$IdArray) . ")"

  9. #9
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,014
    Mentioned
    187 Post(s)
    Tagged
    2 Thread(s)

    not in

    I have never used "NOT IN". I like it! Much more elegant.


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
  •