please i have an issue with codeigniter. when i try to log here is the result:
Fatal error: Call to a member function num_rows() on boolean in D:\xampp\htdocs\procurementSys\application\models\login_model.php on line 19
Below tho code of the relative file:
<?php
class Login_model extends CI_Model {
//this function checks whether the username and the password is in the database or not
public function check_login($username, $password){
$this->db->select('username, password, status');
$array = array('username' => $username, 'password' => sha1($password),'status' => 'active');
$this->db->where($array);
$query = $this->db->get('user');
if($query->num_rows() == 1) // if the affected number of rows is one
{
return true;
}
else
{
return false;
}
}
//this function returns the status of the user to be used in authentication
public function user_login_data($username, $password){
$this->db->select('status');
$array = array('username' => $username, 'password' => sha1($password));
$this->db->where($array);
$query = $this->db->get('user');
if($query->num_rows() == 1) // if the affected number of rows is one
{
$row = $query->row();
return $row->status;
}
// else
// {
// return false;
// }
}
public function user_role($username){ // this function gets the user's role from the database
$this->db->select('role');
$this->db->where('username', $username);
$query = $this->db->get('user');
$row = $query->row(0);
return $row->role;
}
public function department($username){ // this function gets the user's department from the database
$this->db->select('department');
$this->db->where('username', $username);
$query = $this->db->get('user');
$row = $query->row(0); // returns the first row with an array of objects that is stored in the row variable
return $row->department;
}
public function get_user_id($username){ // this function gets the user's department from the database
$this->db->select('userID');
$this->db->where('username', $username);
$query = $this->db->get('user');
$row = $query->row(0); // returns the first row with an array of objects that is stored in the row variable
return $row->userID ;
}
public function fullname($username){
$this->db->select('firstName, secondName');
$this->db->where('username', $username);
$query = $this->db->get('user');
$row = $query->row(0);
return $row;
// foreach($query->result() as $row) // returns the query as an array of objects
// {
// $data[] = $row; // equates the array of objects to an array variable
// }
//
// return $data;
// }
}
}
?>
I kept searching for a solution and found this post (Call to a member function num_rows() on boolean) . It gave me an idea but no real help. thanks
I got this error with MySQL Strict Mode enabled. When I disable Strict Mode, error disappeared.
To check this mode is already enabled
SHOW VARIABLES LIKE 'sql_mode';
To make it disabled
SET GLOBAL sql_mode = 'NO_ENGINE_SUBSTITUTION';
Something like this for the count:
$this->db->select('id');
$this->db->from('table');
$this->db->where($your_conditions);
$num_results = $this->db->count_all_results();
**Try something like this **
//this function checks whether the username and the password is in the database or not
public function check_login($username, $password){
$this->db->select('username, password, status');
$array = array('username' => $username, 'password' => sha1($password),'status' => 'active');
$this->db->where($array);
$query = $this->db->get('user');
$result = $query->result();
if($result->num_rows() == 1) // if the affected number of rows is one
{
return true;
}
else
{
return false;
}
}
CODEIGNITER send boolean as result if there is not result
for example as per your method
public function check_login($username, $password){
$this->db->select('username, password, status');
$array = array('username' => $username, 'password' => sha1($password),'status' => 'active');
$this->db->where($array);
$query = $this->db->get('user');
if($query->num_rows() == 1) // if the affected number of rows is one
{
return true;
}
else
{
return false;
}
If there is no data in table it send False while we are expecting some object on which we can work so before calling $query->num_rows() check if it is an object or not so code can be like :
$query = $this->db->get('user');
if(is_object($query)){
if($query->num_rows() == 1) // if the affected number of rows is one
{
return true;
}
}else{
return false;
}
You can write like that. It will work smoothly.
if($query !== FALSE && $query->num_rows() == 1) // if the affected number of rows is one
{
return true;
}
else
{
return false;
}
Related
Hi I want to check query result. If it's null I want to send data to DB. In my code even if result is null I can't update database.
$this->db->where('login',$data_db['login']);
$query = $this->db->get('users');
if(is_null($query)) // If Login doesn't exist in DB
{
$this->db->insert('users', $data_db); // Insert into DB
}
}
I was trying to do that in other way but If user doesn't exist I got "Trying to get non object etc"
Other way:
$this->db->where('login',$data_db['login']);
$query = $this->db->get('users');
$row = $query->row();
if($row->login)
{
$this->load->view('content/error');
}
else
{
$this->db->insert('users', $data_db);
}
I suggest read through user guide before you post a question again you may find what your looking for.
Try n $query->num_rows() userguide
public function somefunction($data_db = array()) {
$this->db->where('login',$data_db['login']);
$query = $this->db->get('users');
if($query->num_rows() > 0){
return TRUE;
} else {
$this->db->insert('users', $data_db);
}
}
could always just do this
if (mysql_num_rows($query)==0) {your code if its null}
I had problem running a query and counting the results with this code:
$this->db->get_where('user_tb', array('username' => $username, 'password' => $password) );
$count = $this->db->count_all_results();
And it always return 1 even if the username and password are wrong.
Then I changed my code to:
$sql = "SELECT * FROM user_tb WHERE username = ? AND password = ?";
$this->db->query($sql, array($username, $password));
$count = $this->db->count_all_results();
But the result is still the same.
Then my third and last try, I changed the code to:
$this->db->where('username', $username);
$this->db->where('password', $password);
$this->db->from('user_tb');
$count = $this->db->count_all_results();
Then it works. What are the differences between this three? Why is the last set of code works and the other two did not?
Its because $this->db->get_where(); and $this->db->query(); executed the query and returned the sql result and ended sql execution. After above two call when you are calling $this->db->count_all_results(); it is independent from above two calls. So it returns 1. and in
$this->db->where('username', $username);
$this->db->where('password', $password);
$this->db->from('user_tb');
$count = $this->db->count_all_results();
Execution is done with above three line query builder. So it is working fine.
Try this to see diffrence
$result = $this->db->get_where('user_tb', array('username' => $username,
'password' => $password) );
$count = $this->db->count_all_results();
print_r($result); // you will see it contains all data related to your query.
Without any query when you use $this->db->count_all_results(); which is equivalent to SELECT COUNT(*) AS numrows and will return 1. In your above two cases happening same.
In the 2nd line, you are using array , but in the 3rd line of your code you used single variable to compare column value thats why its working.
To use array in query use IN() operator.
Passing an array to a query using a WHERE clause
Try following code in your model
class Auth_login extends CI_Model
{
public function user_auth($username, $password)
{
$q = $this->db->where(['uname'=>$username, 'password'=>$password])
->get('table'); //your table name
if($q->num_rows())
{
return $q->row()->id; //primary key of returned row
}
else
{
return FALSE;
}
}
}
Add this code to the controller to authenticate user
$username = $this->input->post('user');
$password = $this->input->post('pwd');
$this->load->model('auth_login');
$login_id = $this->auth_login->user_auth($username, $password);
if($login_id)
{
$this->load->library('session');
$this->session->set_userdata('id',$login_id);
return redirect('user/dashboard');
}
else
{
$this->session->set_flashdata('feedback','password not match!');
}
return $this->load->view('index_login');
Hello try this code and it return's number of affected rows
$this->db->get_where('user_tb', array('username' => $username, 'password' => $password) );
$query = $this->db->get();
$rowcount = $query->num_rows();
and let me know it's working or not.
I am having issues in updating a table in my database (MySQL workbench).
The code in my model is the following:
function updateMail($new) {
$data = array(
'email' => $new
);
$this->db->where('email', $this->session->userdata('email'));
$result = $this->db->update('person', $data);
$error = $this->db->error();
return $error;
}
My controller then places the return value in $result, and chechs if(!isset($result)).
the problem is that sometimes the table updates, sometimes it doesn't,
but the error is always set.
The table basically contains persons with an id, name, firstname, password, username, email, and patient field.
Am I doing something wrong? Or is there a way a can display the error message it throws?
Instead of error you have to check number of affected row in your query
function updateMail($new) {
$data = array(
'email' => $new
);
$this->db->where('email', $this->session->userdata('email'));
$result = $this->db->update('person', $data);
$afftectedRows = $this->db->affected_rows();
if ($afftectedRows > 0) {
return TRUE;
} else {
return FALSE;
}
}
I am getting very frustrated. I have two functions which have similar "instructions" ie: return values from a users table in the database.
The second one works fine, however the first one is returning an empty value.
Here is the code:
public function ValidateUser($username, $password)
{
$stmt = "SELECT password FROM users WHERE username = :username LIMIT 1";
if(!($grabUser = $this->db->prepare($stmt)))
{
return null;
}
$grabUser->bindParam(":username", $username, PDO::PARAM_STR);
$grabUser->execute();
$data = $grabUser->fetch();
if(count($grabUser->fetchColumn()) <= 0)
{
return null;
}
echo $data['password'].'s';
if(!password_verify($password,$data['password']))
{
return null;
}
return $this->core->encrypt($data['password']);
}
I'm trying to display the $data['password'] on the page just to test whether it returns a value from the database, however it is simply returning empty, whereas the query is returning a column because it passes the
if(count($grabUser->fetchColumn()) <= 0)
{
return null;
}
condition.
The $username and $password variables are both set, so they are no problem.
Just in case you ask, this is the function that does work properly:
public function ValidateFacebookUser($email, $fid)
{
$stmt = "SELECT username, password FROM users WHERE email_address = :email AND connected_fb = '1' AND connected_fb_id = :fb LIMIT 1";
if(!($grabUser = $this->db->prepare($stmt)))
{
return null;
}
$grabUser->bindParam(":email", $email, PDO::PARAM_STR);
$grabUser->bindParam(":fb", $fid, PDO::PARAM_INT);
$grabUser->execute();
$data = $grabUser->fetch();
if(count($grabUser->fetchColumn()) <= 0)
{
return null;
}
return array($data['username'], $this->core->encrypt($data['password']));
}
It does turn the username and password for that case. Why does it not work in the first function?
Thanks.
No, you shouldn't mix up ->fetchColumn and ->fetch and with LIMIT 1.
What happens is that you already ->fetch() the first row. After that invocation of ->fetchColumn(), there's no more row to fetch.
public function ValidateUser($username, $password)
{
$stmt = "SELECT password FROM users WHERE username = :username LIMIT 1";
if(!($grabUser = $this->db->prepare($stmt))) {
return null;
}
$grabUser->bindParam(":username", $username, PDO::PARAM_STR);
$grabUser->execute();
$data = $grabUser->fetch(); // fetch once
// no need to add ->fetchColumn checking
$ret = null;
if(!empty($data['password']) && password_verify($password,$data['password'])) {
$ret = $this->core->encrypt($data['password']);
}
return $ret;
}
Look at the manual for fetchColumn(). You can see that this fetches the next result set. So if you've already called fetch(), there should be no next result set as per your code:
$data = $grabUser->fetch();
if(count($grabUser->fetchColumn()) <= 0)
{
return null;
}
This will always return null with a LIMIT 1 or single row result.
I'm currently making a validation process to check the old password of the user but problem is I can't get it why my query returns zero rows where expected it to have 1 row. 1 more thing is that even I don't convert my password text into md5 the hashed password still get the correct answer and I don't know why it happens. here's my code so far:
public function validate_oldpassword($username,$password)
{
$this->db->select('user_salt');
$query = $this->db->get_where('login',array('username' => $username));
if ($query->num_rows() == 1)
{
foreach ($query->result() as $row)
{
$password2 = hash("sha256",$password.$row->user_salt);
$this->db->flush_cache();
$query = $this->db->get_where('login', array('username' => $username, 'password' => $password2));
//return $this->db->last_query();
return $query->num_rows();
if($query->num_rows()==1){
return "YEHEY";
}else{
return "NO";
}
}
}
}
I also checked the query and it returns the correct query which is:
SELECT * FROM (`login`) WHERE `username` = 'kahel' AND `password` = 'f91d20d381426ea56e57da9ab23d0c568b8f934c3ff313e1bbf6c28a4fee758a'
My password is test salt is XgvT7F~(CYr#*0E1^UI#xkqJ5GcAO8BHsotZpf+WQ!4&ja2y%NdelLmhPSnRw9)zDK63VMuib and stored password is f91d20d381426ea56e57da9ab23d0c568b8f934c3ff313e1bbf6c28a4fee758a
what bothers me the most is that in my login page function everything works just fine. Here's my login code function:
public function get_login_credentials($username,$password)
{
$this->db->select('user_salt');
$query = $this->db->get_where('login',array('username' => $username));
if ($query->num_rows() == 1)
{
foreach ($query->result() as $row)
{
$password = hash("sha256",md5($password).$row->user_salt);
$query = $this->db->get_where('login', array('username' => $username, 'password' => $password));
return $query->num_rows();
}
}
}
I can't really figure it out why it returns zero rows.
In the login function you are setting:
$password = hash("sha256",md5($password).$row->user_salt);
and in validate_oldpassword you are setting:
$password2 = hash("sha256",$password.$row->user_salt);
you aren't doing the md5 hash on it