Welcome Guest, Not a member yet? Register   Sign In
$query->num_rows nothing returns
#1

Within my model I have the following code but does not return anything (running the sql in the bank returns the record).
 
I also tried with $ query-> num_rows also, same thing ... what's wrong?

Code:
<?php
class login_model extends CI_Model{

    function ValidaLogin()
    {

        $query = $this->db->query("SELECT * FROM `usuario` WHERE `Usuario` = 'user' AND `Senha` = 'password'");
        
        if ($query->num_rows() > 0)
        {
           foreach ($query->result() as $row)
           {
              echo $row->usuario;
           }
        }
Reply
#2

(07-20-2015, 07:05 PM)mlfgo Wrote: Within my model I have the following code but does not return anything (running the sql in the bank returns the record).
 
I also tried with $ query-> num_rows also, same thing ... what's wrong?


Code:
<?php
class login_model extends CI_Model{

function ValidaLogin()
{

$query = $this->db->query("SELECT * FROM `usuario` WHERE `Usuario` = 'user' AND `Senha` = 'password'");

if ($query->num_rows() > 0)
{
  foreach ($query->result() as $row)
  {
     echo $row->usuario;
  }
}

Maybe you should add value for "FALSE" logic. Like this :
if($query->num_rows() > 0){
 //code if result not empty
}else{
  return FALSE;
}
Reply
#3

As I see, your field name in the table is `Usuario`. But when you call it, you do it with 'usuario'.
Reply
#4

If it's true that your table name is in lower case and the fields are in firstcaps, then the next question is: do you really want to look for records that literally hold the values 'user' and 'password', or - which is more likely - do you want to get records that match a certain combination of username and password?
In that case, your query should look like this:
PHP Code:
$query $this->db->query("SELECT * FROM `usuario` WHERE `Usuario` = '$user' AND `Senha` = '$password'"); 
Reply
#5

Thanks friends.

Remodeling the question, the instruction
Code:
$query->num_rows;
returns 0, where using CodeIgniter2, with the same database and sql, returns 1.

is some setting?
Reply
#6

(07-22-2015, 05:15 AM)mlfgo Wrote: Thanks friends.

Remodeling the question, the instruction
Code:
$query->num_rows;
returns 0, where using CodeIgniter2, with the same database and sql, returns 1.

is some setting?

There's a difference between $this->num_rows and $this->num_rows() ... you should use the latter.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB