codeigniter error with selecting data from table

2019-09-08 04:12发布

问题:

I cant figure out whats wrong with my model, it said a Fatal Error occured.

Here's my model file:

function getName($no_ktp){
    $this->db->select('nama')->from('dt_prbd')->where('no_ktp', $no_ktp);
    $qry_getName = $this->db->get();

    if ($qry_getName->num_rows() > 0) {
        foreach ($qry_getName->result() as $data_getName){
            $hasil_qry_getName[] = $data_getName;
        }
        return $hasil_qry_getName;
    }
}

I got this error.

Fatal error: Call to a member function num_rows() on boolean in C:\xampp\[APP_PATH]\M_hrd_apps.php on line 25

I thought the error is in the query, so I changed it into this:

$qry_getName = 
$this->db->select('nama')
->from('dt_prbd')
->where('no_ktp', $no_ktp)
->get();

but the error is the same,

Call to a member function num_rows() on boolean

Can anyone help me please?

回答1:

Tr this

function getName($no_ktp){
     $this->db->select('nama')->from('dt_prbd')->where('no_ktp', $no_ktp);
      $qry_getName = $this->db->get();
       $result = $qry_getName->result();
       return $result;
    }
}


回答2:

Try this

In Model

function getName($no_ktp)
{
    $query = $this->db->query("SELECT nama FROM dt_prbd WHERE no_ktp= '$no_ktp'");
    $result = $query->result_array();
    $count = count($result);

    if (empty($count)) 
    {
        $log = 0;
        return $log;
    }
    else
    {
        return $result;
    }
}

In controller

$result = $this->Model_name->getName();

if($result == 0)
{
    echo 'Data Is empty';
}
else
{
    $data['name'] = $result;
    #rest of your code
}

EDIT 01

To configure your database

go to config/database.php in bottom of page define

  1. Databse name
  2. Mysql Username
  3. Mysql Password


回答3:

all problem solved, there is no error in code. i try to re-create the db, and then all script turn out like what i expect.

thanks for all help Deep Parekh and Abdulla