Welcome Guest, Not a member yet? Register   Sign In
Database error, query mixup
#1

[eluser]angelleye[/eluser]
Database error:
Having a strange thing happen here with using multiple db->where statements on a single controller. Several queries are within one controller, somehow a 'where' statement from one function is getting mixed in with another query in a different function.

I looked into the db cache commands and checked my main config file. The db cache is not in use and after experimenting with the flush commands there was no difference in the result.

Below are the two functions that are getting crossed, in the order they appear. Any information on how to resolve this issue would be greatly appreciated.

Thank you.

--

//1st function:

// Lookup user's grid settings/filters
function getGridLimit()
{
// Flush Active Record Cache
$this->db->cache_delete_all();

$this->db->limit("1");
$this->db->orderby("Creation_DateTime", "DESC");
$this->db->where("User_Id", $this->tank_auth->get_user_id());
$this->db->where("Setting_Type", "Grid");
$this->db->where("Grid_Id", "Members");
$query = $this->db->get('user_settings');
if ($query->num_rows() > 0)
{
foreach ($query->result_array() as $row)
{
return $grid_limit_set = $row['Grid_Limit'];
}
}
else
{
return $grid_limit_set = "50";
}
}

//2nd function:

// Get All Members
function getUsers()
{
// Get ID from GET
$record_id = isset($_GET['id']) ? $_GET['id'] : 'none';
if($record_id != 'none')
{
$this->db->where("id", $record_id);
}
else
{
// Do not include the admins (99)
$this->db->where("access_level !=", "99");
}
// If we load Detail view, but don't have an id, then redirect
if($record_id == '' && $sub != 'default')
{
redirect('members');
}
elseif($record_id == 'none' && isset($_GET['sub']) && $_GET['sub'] == 'detail')
{
redirect('members');
}
else
{
// CI Pagination Settings
$offset=$this->uri->segment(3);
$limit = $this->getGridLimit();
if($limit != 0)
{
$this->db->limit($limit);
}
$this->db->offset($offset);

// Always order by the Creation DateTime
$this->db->orderby("users.created", "DESC");
$this->db->select('*');
$this->db->from('users');
return $this->db->get();
}
// Flush Active Record Cache
$this->db->flush_cache();
}

--

And this is the resulting error / query:

Error Number: 1054
Unknown column 'access_level' in 'where clause'

SELECT * FROM (`user_settings`) WHERE `access_level` != '99' AND `User_Id` = '3' AND `Setting_Type` = 'Grid' AND `Grid_Id` = 'Members' ORDER BY `Creation_DateTime` DESC LIMIT 1

--

WHERE `access_level` != '99' ( Should not be part of this query)

//END
#2

[eluser]InsiteFX[/eluser]
I think you need to read the CodeIgniter Users Guide!

Your database queries should be in a model not your controller!

InsiteFX




Theme © iAndrew 2016 - Forum software by © MyBB