Welcome Guest, Not a member yet? Register   Sign In
[Closed] error Undefined variable: query
#1

[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();
}
#2

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

[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?



#4

[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.
#5

[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!
#6

[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
#7

[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");

#8

[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;
}




Theme © iAndrew 2016 - Forum software by © MyBB