查看数据库中的数据无法使用PDO

时间:2016-06-08 03:37:46

标签: php mysql pdo

我有一个代码,用于查看我的数据库中的所有数据,代码在sql语句中没有WHERE的情况下工作正常,但是当我尝试操作sql并在其中放入WHERE子句时当我回应它时,它会给我一个“0”值。

这是我的代码

user.php的

public function province($query)
{
    $stmt = $this->db->prepare($query);
    $result = $stmt->fetch(PDO::FETCH_ASSOC);

        echo "<td>" .$result['id']." </td>";
        echo "<td>" .$result['area']. "</td>";
}

的index.php

<?php
     $query="SELECT COUNT(Province) AS id, FORMAT((SUM(Area)),0) AS area FROM rlbet WHERE Province='ALBAY' AND Year_Approved='2014'";
     $LTID->province($query);
?>

<?php
     $query="SELECT COUNT(Province) AS id, FORMAT((SUM(Area)),0) AS area FROM rlbet WHERE Province='CAMARINES NORTE' AND Year_Approved='2014'";
                    $LTID->province($query);
?>

下面是原始代码 正在运行的原始代码

public function province()
{
$stmt = $this->db->query("SELECT COUNT(ID) AS id, FORMAT((SUM(ID)),0) AS total from rlbet");

$result = $stmt->fetch(PDO::FETCH_ASSOC);

    echo "<td>" .$result['id']." </td>";
    echo "<td>" .$result['area']. "</td>";
}

的index.php 原始代码

<?php
     echo $LTID->province();
?>

错误

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42000]: Syntax error or access violation: 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 'FORMAT((SUM(Area)),0) AS area FROM rlbet WHERE Province='CAMARINES NORTE' AND Ye' at line 1' in D:\wamp\www\LTID\class.user.php on line 130

1 个答案:

答案 0 :(得分:-1)

试试这个:

public function province($province, $year_approved)
{
$stmt = $this->db->query("SELECT COUNT(ID) AS id, FORMAT((SUM(ID)),0) AS total from rlbet WHERE Province=:Province AND Year_Approved=:YearApproved");
$stmt->bindValue(':Province', $province, PDO::PARAM_STR);
$stmt->bindValue(':YearApproved', $year_approved, PDO::PARAM_STR);
$result = $stmt->fetch(PDO::FETCH_ASSOC);

echo "<td>" .$result['id']." </td>";
echo "<td>" .$result['area']. "</td>";
}


<?php
province('ALBAY', '2014');
province('CAMARINES NORTE', '2014');    
?>