Codeigniter,其中条件在$ this-> query()中为空

时间:2018-12-21 11:55:14

标签: php mysql codeigniter

我现在正面临非常奇怪的状况。我在CodeIgniter中用如下WHERE条件编写了一个查询:

$queryps = $this->db->query("SELECT count(workorderno) as total from crm_workorder where workorderno =".$sitecode.""); 

但是我收到此错误:

  

您的SQL语法有错误;检查手册   对应于您的MySQL服务器版本以使用正确的语法   在第1行的“”附近

     

从crm_workorder中选择count(workorderno)个总数,其中   workorderno =

现在很奇怪的是,变量$sitecode不为空。当我回显查询时,它显示以下内容:

SELECT count(workorderno) as total from crm_workorder where workorderno =2

但是在SQL查询中,我遇到了以上错误。 WHERE条件中没有任何内容。

我尝试了所有可能的方法来找出其背后的原因,但我无法弄清楚。谢谢。

4 个答案:

答案 0 :(得分:1)

这就是您所需要的,它必须在您的模型中。

<?php

    $this->db->select("SELECT count(workorderno) as total");
    $this->db->from("crm_workorder");
    $this->db->where("workorderno",$sitecode);

    $queryps = $this->db->get();

?>

答案 1 :(得分:0)

$queryps = $this->db->query("SELECT COUNT(workorderno) AS total FROM crm_workorder WHERE workorderno=$sitecode");

并确保$ sitecode有一个值。

测试

答案 2 :(得分:0)

尝试

$this->db->select('count(workorderno) as total');
$this->db->from("crm_workorder");

$this->db->where("workorderno",$sitecode);


$query = $this->db->get();

if ( $query->num_rows() > 0 )
{
    $row = $query->row_array();
    print_r($row);
}

答案 3 :(得分:0)

确定,尝试此代码。您的错误将得到解决。

$queryps = $this->db->query("SELECT count(workorderno) as total from crm_workorder where workorderno ='$sitecode'");