Thread: Array Question.

    #1
  1. A Change of Season
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    3,189
    Rep Power
    220

    Array Question.


    Hello;

    According to CI docs, I could pass an array to CI and use IN phrase!
    How come I get this error?

    But I get this:
    Code:
    Array ( [0] => andrew@yahoo.com [1] => andrew22@yahoo.com )
    
    There was a system error. Please try again!
    
    Severity: Notice
    
    Message: Array to string conversion
    
    Filename: database/DB_driver.php
    
    Line Number: 604
    
    ===========
    
    Error Number: 1064
    
    You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Array AND item_type = 'profiles'' at line 1
    
    SELECT * FROM shared_items WHERE email IN Array AND item_type = 'profiles'
    PHP Code:
    $users_emails_list=array();
    foreach(
    $query->result_array() as $emails =>$email)
        {
            
    $users_emails_list[]=$email['email'];
        }
    print_r($users_emails_list);
    foreach(
    $made_for_me_items as $item)
    {
        
    $sql "SELECT * FROM shared_items WHERE email IN ?
                AND item_type = ?"
    ;
        
    $query $this->db->query($sql, array($users_emails_list$item));
        
    print_r($query->num_rows());

    I understand having sql in the loop is a Taliban practise. For now just go with it.

    Thanks
  2. #2
  3. Lazy Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    16,334
    Rep Power
    9645
    Only thing I can think of is that you're using an outdated version of CI which doesn't support arrays.
  4. #3
  5. A Change of Season
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    3,189
    Rep Power
    220
    I am using this version.

    What do you think is the best way to escape IN phrase?

    Thanks
  6. #4
  7. Lazy Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    16,334
    Rep Power
    9645
    Create the SQL yourself. As in
    PHP Code:
    "IN (" implode(", "array_fill(0/* number of placeholders */"?")) . ")" 
    Then flatten the array of parameter values so it's not nested.
  8. #5
  9. A Change of Season
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    3,189
    Rep Power
    220
    Thanks
  10. #6
  11. A Change of Season
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    3,189
    Rep Power
    220
    Oh wait but I can't see how you're escaping.
  12. #7
  13. Lazy Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    16,334
    Rep Power
    9645
    Use ->query for that. The extra work is just to set up the extra placeholders. You're basically trying to end up with
    PHP Code:
    foreach($made_for_me_items as $item

        
    $sql "SELECT * FROM shared_items WHERE email IN (?, ?, ?, ...) 
                AND item_type = ?"

        
    $query $this->db->query($sql, array($users_emails_list[0], $users_emails_list[1], $users_emails_list[2], ..., $item)); 
        
    print_r($query->num_rows()); 


IMN logo majestic logo threadwatch logo seochat tools logo