CodeIgniter Forums
[Closed] error Undefined variable: query - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: [Closed] error Undefined variable: query (/thread-60567.html)



[Closed] error Undefined variable: query - El Forum - 04-24-2014

[eluser]riwakawd[/eluser]
Hello I have created a library for my users login what gets and updates stuff to and from data base. I am getting a error undefined variable : Query line 8 which in the one in the public function construct.

Not sure if have set the query correct for both public function and function login.

Code:
public function __construct() {
if ($query->num_rows() > 0) {
$this->user_id = $this->db->query('user_id');
$this->username = $this->db->query('username');
$this->db->query("UPDATE " . DB_PREFIX . "user SET ip = '" . $this->db->escape($this->request->server['REMOTE_ADDR']) . "' WHERE user_id = '" . (int)$this->session->userdata['user_id'] . "'");
} else {
$this->logout();
}
}

public function login($username, $password) {
$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "user WHERE username = '" . $this->db->escape($username) . "' AND (password = SHA1(CONCAT(salt, SHA1(CONCAT(salt, SHA1('" . $this->db->escape($password) . "'))))) OR password = '" . $this->db->escape(md5($password)) . "') AND status = '1'");
$ip = $_SERVER['REMOTE_ADDR'];
$query = $this->db->query("UPDATE users SET ip = $ip WHERE user_id = $this->user_id");
if ($users_query->num_rows) {
$this->session->userdata['user_id'] = $user_query->row['user_id'];
$this->user_id = $user_query->row['user_id'];
$this->username = $user_query->row['username'];        
return true;
} else {
return false;
}
}

public function logout() {
$this->session->unset_userdata($user_id);
$this->user_id = '';
$this->username = '';
$this->session->sess_destroy();
}



[Closed] error Undefined variable: query - El Forum - 04-24-2014

[eluser]www.sblog.in[/eluser]
You have used $query inside contructor but how or from where this variable is coming ?


[Closed] error Undefined variable: query - El Forum - 04-24-2014

[eluser]Tim Brownlaw[/eluser]
I'm only going to address the "undefined variable issue".
The rest scares me... ie your constructor!


Now, Is it telling you which variable is undefined?
Are you checking things like if - $this->session->userdata['user_id'] actually exists...
and $this->request->server['REMOTE_ADDR'] exists?






[Closed] error Undefined variable: query - El Forum - 04-24-2014

[eluser]riwakawd[/eluser]
[quote author="Tim Brownlaw" date="1398407924"]I'm only going to address the "undefined variable issue".
The rest scares me... ie your constructor!


Now, Is it telling you which variable is undefined?
Are you checking things like if - $this->session->userdata['user_id'] actually exists...
and $this->request->server['REMOTE_ADDR'] exists?



[/quote]

How would you set it out in library file? I have been looking on internet thats how I got it. I also got token working.


[Closed] error Undefined variable: query - El Forum - 04-24-2014

[eluser]Tim Brownlaw[/eluser]
That's for another day....

You've not answered my question regarding your initial question!

And that's good that you got your tokens working!


[Closed] error Undefined variable: query - El Forum - 04-24-2014

[eluser]riwakawd[/eluser]
[quote author="Tim Brownlaw" date="1398408448"]That's for another day....

You've not answered my question regarding your initial question!

And that's good that you got your tokens working![/quote]

I created separate auth library


[Closed] error Undefined variable: query - El Forum - 04-25-2014

[eluser]InsiteFX[/eluser]
If he is accessing the database in a Library then he needs to get the CI Super object.

Code:
private $_ci;   // The CI Super object

public function __construct()
{
  $this->_ci = get_instance();
}

// Then he needs to use the methods like so

$this->_ci->session->set_userdata($data);


$query = $this->_ci->db->query("UPDATE users SET ip = $ip WHERE user_id = $this->user_id");




[Closed] error Undefined variable: query - El Forum - 04-25-2014

[eluser]riwakawd[/eluser]
[quote author="InsiteFX" date="1398437915"]If he is accessing the database in a Library then he needs to get the CI Super object.

Code:
private $_ci;   // The CI Super object

public function __construct()
{
  $this->_ci = get_instance();
}

// Then he needs to use the methods like so

$this->_ci->session->set_userdata($data);


$query = $this->_ci->db->query("UPDATE users SET ip = $ip WHERE user_id = $this->user_id");

[/quote]

Have been watching some more videos on how to get session data I think this is OK just to grab the username and ip address just so can store the ip in user. I ts going to be easier when I set up my maintenance config in future.

Code:
function get_db_session_data() {
$query = $this->db->select('user_data')->get('ci_sessions');
$user = array(); /* array to store the user data we fetch */
foreach ($query->result() as $row) {

    $getdata = unserialize($row->user_data);
    $user['username'] = $getdata['username'];
    $user['ip_address'] = $getdata['ip_address'];
    $user['is_logged_in'] = $getdata['is_logged_in'];
}
    return $user;
}