CodeIgniter Forums
Problem getting results - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Problem getting results (/showthread.php?tid=9108)



Problem getting results - El Forum - 06-12-2008

[eluser]old_guy[/eluser]
Hello,
I'm a ci newbie and fairly new to PHP/MySql as well. I'm playing around with a simple login structure using controller to call model and then view. I developed the logic first in PHP and now trying to get the same results using CI.

My problem is that my query does not return any rows. It does using non-CI php/mysql. I'm using the profiler function and it shows that my post data is correct and so is my query string. As far as I can tell the CI code is correct but I can't figure out why I'm not getting any data returned from the dB.

Below is my model code:

<?
class Login extends Model {

function login()
{
parent::Model();
}
function ck_name()
{
$username = $this->input->post('username');
$password = $this->input->post('password');

$this->load->database();
$query = $this->db->get_where('auth_users',array('username' => $username, 'password' => $password)) ;

if ($query->num_rows() > 0)
{
foreach ($query->result() as $row)
{
$un = $row->username;
$pw = $row->password;

if ($un == $username and $pw == $password)
{
return 'yes';
}
}
}else{
return 'Username and/or Password not recognized - Please try again.';
}
}
}

?>

Currently the only result I can get passed back to my controller page is the login error message.

Would appreciate any insight into this anyone might have. Thanks!


Problem getting results - El Forum - 06-12-2008

[eluser]chmod[/eluser]
if i was you,i can write the the below:
controller:
$username = $this->input->post('username');
$password = $this->input->post('password');

$result = $this->model->check_user($username,$password);
if ($result)
{
//login
}else{
echo "Username and/or Password not recognized - Please try again.";
}
model:
class Login extends Model {

function login()
{
parent::Model();
$this->load->database();
}
function check_user($username,$password)
{

$query = $this->db->get_where('auth_users',array('username' => $username, 'password' => $password)) ;

if ($query->num_rows() > 0)
{
foreach ($query->result() as $row)
{
$un = $row->username;
$pw = $row->password;

if ($un == $username and $pw == $password)
{
return true;
}
}
}else{
return false;
}
}
}


Problem getting results - El Forum - 06-12-2008

[eluser]Pascal Kriete[/eluser]
...riiiiight.

Old_guy, put this:
Code:
echo $this->db->last_query();
after the get_where and compare it to what you used to run without CI. Are there any differences?


Problem getting results - El Forum - 06-19-2008

[eluser]old_guy[/eluser]
Thanks for the help. When I finally got time to get back to it I found that the problem was in my sql statement. So got the login function working fine.