EllisLab text mark
Advanced Search
     
Problems with ActiveRecord: where($array)
Posted: 21 July 2008 04:37 PM
Avatar
Joined: 2007-08-29
94 posts

Hi,

I use ActiveRecord for querying my databases. I implemented my own function to check if a row exists in the database already.

Basically it works when I pass just one parameter as “where”:

$query $this->db->select($col)->from($tbl)->where($val)->get(); 

But when I pass an associative array as where-parameter, it returns “no row found”.

My array $var looks like this:

$var = array (
                
'passport' => $data['passport'],
                
'country'  => $data['country']
      
); 

I checked if the array passed to the function is filled and it is. Is this a bug in 1.6.3 or am I hving a mistake?

Thanks for your help,
Ben.

 Signature 

Visit the german-speaking CI-community at http://codeigniter.ch
FreeBSD- and Server-Tutorials at http://serverzeit.de
The event-calendar for Mini-Drivers at http://miniyourlife.com

 
Posted: 21 July 2008 05:49 PM   [ # 1 ]   [ Rating: 0 ]
Joined: 2008-03-05
117 posts

You might want to look at the user’s manual for get_where

$query $this->db->get_where('mytable', array('id' => $id), $limit$offset); 

It will do a much better job of explaining than i ever could, and i’m a bit of a newbie myself and don’t want to send you on a wild goose-chase through your code.

 
Posted: 22 July 2008 01:42 AM   [ # 2 ]   [ Rating: 0 ]
Avatar
Joined: 2007-07-30
2144 posts
if_exists($params = array()) {
  
if ((count($params) > 0) && (is_array($params))) {
    $query 
$this->db->get_where('table'$params10);
    if (
$query->num_rows() > 0{
      
return TRUE;
    
}
  }
  
return FALSE;
 Signature 

Follow me on twitter here.
MichaelWales.com | MichaelWales.info

 
Posted: 22 July 2008 03:08 AM   [ # 3 ]   [ Rating: 0 ]
Avatar
Joined: 2007-08-29
94 posts

Ok, I see. Thank you for your help.

But there is one paragraph which confuses me (I followed this paragraph):

“Associative array method:  $array = array(‘name’ => $name, ‘title’ => $title, ‘status’ => $status);

$this->db->where($array);

// Produces: WHERE name = ‘Joe’ AND title = ‘boss’ AND status = ‘active’”

Is this just not working in chaining or not working in general?


EDIT:

Seems Im using it wrong:

$query $this->db->select('id')->get_where('mytable'$var10); 

This doesnt work. How can I make it work with chaining?

 Signature 

Visit the german-speaking CI-community at http://codeigniter.ch
FreeBSD- and Server-Tutorials at http://serverzeit.de
The event-calendar for Mini-Drivers at http://miniyourlife.com

 
Posted: 22 July 2008 01:46 PM   [ # 4 ]   [ Rating: 0 ]
Avatar
Joined: 2007-08-29
94 posts

Ok, got it to work like that:

$query $this->db->select($col)->from($tbl)->get_where($tbl$val10); 
 Signature 

Visit the german-speaking CI-community at http://codeigniter.ch
FreeBSD- and Server-Tutorials at http://serverzeit.de
The event-calendar for Mini-Drivers at http://miniyourlife.com