代码点火器模型包含以下两种方法。
public function generate_uuid() {
return "it is returning SOME unique ID";
}
public function getUniqueIdentifier()
{
$Uuid = '';
$checkStatus=true;
while($checkStatus)
{
$Uuid = $this->generate_uuid();
$checkQry2 = "select * from Leads where controllNumber=".$this->db->escape($Uuid);
$checkRes2 = $this->db->query($checkQry2);
$checkRes2 = $checkRes2->num_rows();
if($checkRes2 === 0)
{
$checkStatus=false;
}
}
return $Uuid;
} 我已经在控制器上导入了这个模型,并从控制器的某个方法调用了if?
$this->modObject->getUniqueIdentifier();错误消息:调用布尔值上的成员函数num_rows()
有人能帮忙吗?谢谢。
发布于 2019-08-09 11:08:50
如果MySQL桥返回错误,则查询方法返回false。编辑:也许您忘记用单引号包围$this->db->转义($Uuid),因为它返回一个字符串。
我建议您使用CodeIgniter的查询生成器,因为它更安全。
您的查询将类似于:
$this->db->from('Leads');
$this->db->where('controllNumber', $Uuid); // No need to escape
$results = $this->db->get()->result();
if ( !empty($results) ) {
$num_rows = count($results);
// Do your stuff
} else {
// No rows returned
}问候
发布于 2019-08-09 17:34:10
在“读取”类型数据库操作中,query()在成功时返回一个对象(类型为CI_DB_result),如果查询失败,则返回布尔false。在此上下文中,不生成任何行的查询不是失败。
在我看来,如果数据库中尚未使用由getUniqueIdentifier()生成的值,那么generate_uuid()将返回唯一It。如果已经使用了,则应该返回false。以下将完成这一任务。
在这里,我们使用查询绑定的query()方法来确保安全转义值。
public function getUniqueIdentifier()
{
$Uuid = $this->generate_uuid();
$sql = "select * from Leads where controllNumber = ?"; //two 'l' in 'controll'?
$checkRes = $this->db->query($sql, array($Uuid));
//If the query did not fail and we retrieved no rows return the unique id
if($checkRes && $checkRes->num_rows() === 0)
{
return $Uuid;
}
return false;
} 而不是要求所有列(即select * ...'),只要求一个列(即'select controllNumber ...' )可能会产生更快的查询。
https://stackoverflow.com/questions/57428712
复制相似问题