从表中选择数据时出现codeigniter错误

时间:2015-10-26 02:59:38

标签: php codeigniter

我无法弄清楚我的模型有什么问题,它说发生致命错误。

这是我的模型文件:

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;
    }
}

我收到了这个错误。

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

我认为错误在查询中,所以我将其更改为:

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

但错误是一样的,

Call to a member function num_rows() on boolean

有人可以帮我吗?

3 个答案:

答案 0 :(得分:0)

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;
    }
}

答案 1 :(得分:0)

试试这个

在模型中

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;
    }
}

在控制器中

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

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

编辑01

配置数据库

转到页面底部的config/database.php定义

  1. 数据库名称
  2. Mysql用户名
  3. Mysql密码

答案 2 :(得分:0)

所有问题都解决了,代码中没有错误。我尝试重新创建数据库,然后所有脚本都像我期望的那样结束。

感谢所有帮助Deep ParekhAbdulla