Count results from a select in Kohana

Asked

Viewed 91 times

1

Would you like to know how I count the results of a select in Kohana? I have this code:

public function action_verification() {
    $login = $this->request->post('login');
    $senha = md5($this->request->post('senha'));
    //$result = ORM::factory('usuario')->where('login', '=', $login)->and_where('senha', '=', $senha)->find_all();
    $result = DB::select('*')
            ->from('usuarios')
            ->where('login', '=', $login)
            ->and_where('senha', '=', $senha)
            ->as_object()
            ->execute()
            ->current();
    $session = Session::instance();        
    if (count($result) == 0) {
        $session->set('erro', "Usuário ou senha incorreto!");
        $this->redirect('login');
    } else {
        if ($result->codCliente != 0) {                
            $session->set('nome', $result->login);
            $session->set('active',TRUE);
            $this->redirect('AdminHelpdesk/home');
        } else {
            $this->redirect('admin/home');
        }
    }
}

Only it always returns me 1. If I leave only this:

$result = DB::select('*')
            ->from('usuarios')
            ->where('login', '=', $login)
            ->and_where('senha', '=', $senha)
            ->as_object()
            ->execute();

Then yes it returns me 1 or 0, only doing so it results me in this mistake:

Errorexception [ Notice ]: Undefined Property: Database_mysql_result::$codCliente

I mean, he gives me the error in that if:

if ($result->codCliente != 0) {

Can someone give me a hand with that?

1 answer

1

Try this after $session = Session::instance();

 var_dump($result);

And see if $codCliente appears set, if not, for which variable the resulting Id is being set, probably the id is set with a different name between the BD and the object.

  • The name is the same as the BD, and the var_dumpe shows me this: Object(Database_mysql_result)[21] protected '_internal_row' => int 0 protected '_query' => string 'SELECT * FROM usuarios WHERE login = '[email protected]' AND senha = 'e959088c6049f1104c84c9bde5560a13'' (length=112) protected '_result' => Resource(80, mysql result) protected '_total_rows' => int 0 protected '_current_row' => int 0 protected '_as_object' => Boolean true protected '_object_params' => null

  • The result is displaying an object, $result->result is where the Resource is, which is a mysql return type. count($result). It is applying directly to the object, not to mysql return, which does not work. count($result->result), if not, try to check what is stored in this result by modifying the var_dump($result) for var_dump($result->result).

  • If ($result->num_rows > 0) {

Browser other questions tagged

You are not signed in. Login or sign up in order to post.