如何在视图中记录是否为空?

时间:2013-05-09 10:00:35

标签: php sql database codeigniter

在我的codeigniter PHP模型中我有

if  ($this->input->post('questions') != "")
        {
            if($this->input->post('questions') == "Yes")
            {
                $this->db->where('webinar_event.questions !=',"");
                $this->db->where('webinar_event.questions IS NOT ', null, false);
            }
            else
            {
                $this->db->where('webinar_event.questions',"");
                $this->db->where('webinar_event.questions IS', null, true);
            }

然而,当我跑步时  echo $this->db->last_query();我收到此错误

'Error Number: 1064

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 5

SELECT * FROM (`health_professional`) JOIN `webinar_event` ON `webinar_event`.`hpid` = `health_professional`.`hpid` WHERE `webinar_event`.`questions` = '' AND `webinar_event`.`questions` IS

Filename: D:\Development\PfizerWebinar\web\system\database\DB_driver.php

Line Number: 330'  

基本上我要做的就是让它成为一个如果我搜索“问问题”我得到任何不为空的东西如果我想搜索他们是否确实问过一个问题告诉我所有那些不是空。

2 个答案:

答案 0 :(得分:1)

    Try this 


      if  ($this->input->post('questions') != "")
      {
        if($this->input->post('questions') == "Yes")
        {
            $this->db->where('webinar_event.questions !=',"");
            $this->db->where('webinar_event.questions IS NOT NULL', null, false);
        }
        else
        {
            $this->db->where('webinar_event.questions',"");
            $this->db->where('webinar_event.questions IS NULL', null, true);
        }
      }

答案 1 :(得分:0)

if(!empty($var)){
  //do stuff
}

这里我们检查变量是否为空因此!    或者您可以使用:

if(isset($var)){
 //do stuff
}

检查变量是否已设置。 或者:

if(is_null($var)){

}else{
  //do stuff
}

无论你喜欢什么,或者isset和!empty的组合通常都是我想要的。