检查表记录是否存在于codeigniter中?

时间:2018-07-23 12:20:19

标签: php mysql

我正在尝试使脚本检查SQL表中是否已存在

类似这样的东西:

我有一个名为amx_amxadmins的表,并希望检查用户flag是否存在,请标记为show this,但我的脚本将一直显示标记为

$username = $this->user->getUsername($this->session->userdata('userid'));
$query2 = $this->db->query("SELECT `flags`, `username` FROM `".$amxadmins."` WHERE `flags`='a' AND `username`='".$username."' ");
 $row2 = $query2->row();
  if ($row2 != "a")
    {
      echo "If already flag a ";
    }else { 
      echo "If not "; 
    } 

3 个答案:

答案 0 :(得分:0)

不比较整个对象。比较一个字段值,如下所示

if ($row2->username != "a")
    {
      echo "If already flag a ";
    }else { 
      echo "If not "; 
    } 

答案 1 :(得分:0)

您已从db获取了一行,因此您需要检查行对象是否为空。

if ($query2->num_rows()>0)
{
  echo "If already flag a ";
}else { 
  echo "If not "; 
} 

在条件中使用以上代码。

答案 2 :(得分:0)

谢谢大家。我这样解决了问题:

<?php
$username = $this->user->getUsername($this->session->userdata('userid'));
$query2 = $this->db->query("SELECT `auth`, `flag_a` FROM `".$amxadmins."` WHERE `flag_a`='0' ORDER BY `auth`='".$username."' ");
if ($query2->num_rows() != 0)
{
  echo "If already flag a ";
}else { 
  echo "If not "; 
}       
?>