The following code is issuing the following error message:
A Database Error Occurred
Error Number: 1066
Not unique table/alias: 'users'
SELECT * FROM (`users`, `users`) JOIN `user_profiles` ON `users`.`id` = `user_profiles`.`user_id`
Filename: /home/xtremer/public_html/kowmanager/models/cpanel/dashboard.php
Line Number: 38
Here is my code:
class Dashboard extends CI_Model {
private $table_name = 'users'; // user accounts
private $profile_table_name = 'user_profiles'; // user profiles
function __construct() {
parent::__construct();
$ci =& get_instance();
$this->table_name = $ci->config->item('db_table_prefix', 'tank_auth').$this->table_name;
$this->profile_table_name = $ci->config->item('db_table_prefix', 'tank_auth').$this->profile_table_name;
}
/**
* Get user info by Id
*
* @param int
* @param bool
* @return object
*/
function get_user_info($id) {
$this->db->select('*');
$this->db->from('users');
$this->db->join('user_profiles', 'users.id = user_profiles.user_id');
$query = $this->db->get($this->table_name);
if ($query->num_rows() == 1) {
return $data = $query->row();
} else {
return NULL;
}
}
}
EDIT: I know have this for my function and changed it because after looking at it I didn’t need the extra table. I have an array ($data) of values being sent to my header but can I get away with sending it to the build that way it sends the array to all the partials.
function get_user_info($id)
{
$this->db->select('*');
$this->db->from('users');
$this->db->where('users.id', $id);
$query = $this->db->get();
if ($query->num_rows() == 1)
{
return $data = $query->row();
}
else
{
return NULL;
}
}
And this is from my controller I updated:
function index()
{
$id = $this->tank_auth->get_user_id();
$data = $this->Dashboard->get_user_info($id);
print_r($data);
$this->template->set_layout('cpanel')->enable_parser(false);
$this->template->set_partial('header', 'partials/header', $data);
$this->template->set_partial('sidebar', 'partials/sidebar');
$this->template->set_partial('content', 'partials/content');
$this->template->set_partial('footer', 'partials/footer');
$this->template->build('/cpanel/index');
}
The problem is this:
I assume you’re trying to join the table to itself, but you can’t do that without giving aliases to the tables, otherwise the database doesn’t know which side of the join you’re referring to when you reference fields from it.
Try this:
Update:
My above answer was written based on the error message, but I didn’t notice the code link (I’ve since added the code to be inline with the question).
I’m not overly familiar with CI, but I have a feeling your problem lies in these three lines:
private $table_name = 'users';$this->db->from('users');$query = $this->db->get($this->table_name);In line 3, you’re explicitly
getting$this->table_namewhich points to theuserstable as shown in line 1). However, by explicitly setting afromtable, also pointed tousers, in line 2, I think you’re accidentally setting up a join between two tables. Since these two tables are both the same, and neither is aliased, it is resulting in an error. Try removing the$this->db->from('users');line and seeing if that resolves the issue.Update 2:
I’ve been reading the CodeIgniter user guide, and it seems you don’t need to specify anything as a parameter to
$this->db->get()when you’re using building a query using methods likefrom(). I’d suggest just changing this line:to this:
See this page for details.