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?
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
WHERElogin
= '[email protected]' ANDsenha
= '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– Daniel Swater
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 thevar_dump($result)
forvar_dump($result->result)
.– Marcelo Aymone
If ($result->num_rows > 0) {
– Edilson