Welcome Guest, Not a member yet? Register   Sign In
query->row() not working
#1

[eluser]Kraig[/eluser]
So I have read the guide and I am doing this right, but it's not working. It is something with the model because when I enter in a string with the matching password it works. Any ideas?
If I do this it doesn't work:
Code:
$userID = $this->session->userdata('id');
$curr_password = $this->account_model->getPass($userID);

Works when I do this:
Code:
$userID = $this->session->userdata('id');
$curr_password = 'some hashed password';

Here's my model:
Code:
function getPass($userID) {
  $query = $this->db->query("SELECT * FROM user WHERE id = '$userID'");
  
  if ($query->num_rows() > 0)
  {
     $row = $query->row();
  
     $row = $row->password;
  }
  
  return $row;
}
#2

[eluser]DarkManX[/eluser]
You should learn to debug such easy code. Is $row set in the if-statement? Dump it! If not, is query set? Dump it! Is $userId set? Dump it! Is the model loaded? Is there an user in the db with the given userID?
#3

[eluser]xtremer360[/eluser]
And honestly you really shouldn't need a function to just get the user's password. If anything it should be a getUserData function. Reason being is it'd be easier to access the user's data as a object/array that just trying to get data from each function. Which is easier on your database. Running 1 query vs. having to run how many different queries for each piece of information about the user.
#4

[eluser]Kraig[/eluser]
I would dump it, but this is in an AJAX call to a PHP page and whenI use var_dump it doesn't show anything.
#5

[eluser]xtremer360[/eluser]
You should run echo $this->db->last_query(); right under where the query is being ran and then run that inside your phpmyadmin if that's what you use to see what results get returned.
#6

[eluser]TWP Marketing[/eluser]
Your call to the model function should use the model class name, which is capitalized.
Quote:$curr_password = $this->Account_model->getPass($userID);
#7

[eluser]Aken[/eluser]
If you are able to retrieve the user's password from your database, then you aren't storing it properly. Passwords should be encrypted using one-way hash, and you should never know what they are.

[quote author="TWP Marketing" date="1345917261"]Your call to the model function should use the model class name, which is capitalized.
Quote:$curr_password = $this->Account_model->getPass($userID);
[/quote]
CI looks for both versions - you don't have to capitalize library or model names.
#8

[eluser]Kraig[/eluser]
Yes I know...once I get the hashed password I compare it to the password they entered, by encrypting it before hand. I got it working though...maybe I'm using an old version but having the model all lower case works.
#9

[eluser]boltsabre[/eluser]
Quote:$query = $this->db->query("SELECT * FROM user WHERE id = '$userID'");

Do you really need to get all the data for that row, it's very rare that you do? (I imagine your table rows contains dates such as "date_created", "last_update", "date_deleted", etc, and other stuff that you really don't need to access).

It's rare that you ever need to access all the data from a row, and when you do you are adding extra database load and processing load on your server, get into the habit of only selecting what you need inside that function, which I'm guessing by the name of your function is just their password, so SELECT password...., not SELECT *




Theme © iAndrew 2016 - Forum software by © MyBB