[eluser]stormbytes[/eluser]
QUESTION: What is it about my 'where' statement that's borking everything?
I've got two class methods:
Code:
// Takes given $asset_id & checks if a row exists in the database with that asset_id value
public function get_asset_by_id($asset_id = NULL)
{
if( ! $asset_id)
{
return FALSE;
}
$args['where'] = array('a.id' => $asset_id);
$args['limit'] = 1;
$rows = $this->_read($args);
if($rows->num_rows() == 1)
{
foreach($rows->result() as $row)
{
return $row;
}
}
}
// Reads database & returns rows
private function _read($args = array())
{
$query = "a.id,
a.user_id,
a.title,
a.is_public,
a.is_enabled,
c.category,
u.user_handle";
$this->db->select($query);
if(array_key_exists('where', $args))
{
$this->db->where($args['where']);
}
if(array_key_exists('order_by', $args))
{
$this->db->order_by($args['order_by']);
}
else
{
$this->db->order_by('date_created DESC');
}
if(array_key_exists('limit', $args))
{
$limit = $args['limit'];
}
else
{
$limit = NULL;
}
if(array_key_exists('offset', $args))
{
$offset = $args['offset'];
}
else
{
$offset = NULL;
}
$this->db->join('meta_categories c', 'a.category_id = c.id', 'inner');
$this->db->join('users u', 'a.user_id = u.id', 'inner');
if(($rows = $this->db->get('user_assets a', $limit, $offset))) // CIUG has order of 2nd, 3rd args backwards!
{
return $rows;
}
}
For some reason, get_asset_by_id returns num_rows = 0.
In fact, there *is* a row with the requested asset_id, and it returns this when written this way:
Code:
public function get_asset_by_id($asset_id = NULL)
{
if( ! $asset_id)
{
return FALSE;
}
$args['where'] = array('a.id' => $asset_id);
$args['limit'] = 1;
// Direct DB call
$rows = $this->db->get_where('user_assets', array('id' => $asset_id));
if($rows->num_rows() == 1)
{
foreach($rows->result() as $row)
{
return $row;
}
}
}