【问题标题】:How to get data using SELECT request如何使用 SELECT 请求获取数据
【发布时间】:2019-06-27 16:56:15
【问题描述】:

我正在尝试在我的数据库中显示运营商的统计数据。当我调用该函数但我收到如下错误:

class: "Doctrine\DBAL\Exception\SyntaxErrorException"
message: "An exception occurred while executing 'select count(cod_ser) TOTAL FROM public.tiers_datamart period =06-2019':↵↵SQLSTATE[42601]:

我的代码是这样的:

public function getTotalDatabyMonth($date, $type, $filtre) {
        $em = $this->getDoctrine()->getManager('dbStat')->getConnection();
        if($filtre == null) {
            $restriction = "period =".$date;
        }else {
            $restriction = $type." = ".$filtre." AND period = ".$date;
        }
        $rawSql = "select count(".$type.") TOTAL FROM public.tiers_datamart ".$restriction;        
        $stmt = $em->prepare($rawSql);
        $stmt->execute();   
        $result = $stmt->fetchAll();    
        return intval($result[0]['total']);
    }

    public function getTotalDatabyMonths($dates, $type, $filtre) {
        $totalDatas = [];
        foreach($dates as $date){
            $total = $this->getTotalDatabyMonth($date, $type, $filtre);
            array_push($totalDatas, $total);
        }
        return $totalDatas;
    }

我通过'data'=> $this->getTotalDatabyMonths($dataFormats[1], "cod_ser", $restriction)调用函数

我尝试在方法getTotalDatabyMonths() 中将$filtre 替换为$restriction,但它返回相同的值12 次。

【问题讨论】:

  • 您需要在该语句中使用 WHERE 一词。 select count(cod_ser) TOTAL FROM public.tiers_datamart WHERE period =06-2019
  • 或者... WHERE period = "06-2019"(引用)

标签: php sql symfony


【解决方案1】:

$rawSql = "select count(".$type.") TOTAL FROM public.tiers_datamart WHERE ".$restriction;

添加 WHERE,您就可以开始了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-10-19
    • 1970-01-01
    • 1970-01-01
    • 2020-08-29
    • 1970-01-01
    • 1970-01-01
    • 2019-08-17
    • 1970-01-01
    相关资源
    最近更新 更多