Zend DB - Count(*) 不工作

发布于 2024-07-22 09:30:03 字数 1355 浏览 14 评论 0原文

我的 sql 语句不适用于 Zend,它抱怨 Count(*) 字段...我做错了什么?

// get open/closed
$stmt = $db->query('SELECT status, count(*) as total FROM reported_issues WHERE date_reported >= '.$today.' AND status IN (0,1) GROUP BY status');
while ($row = $stmt->fetch())
{
    switch ($row['status'])
    {
        case 0:
            $totalIssuesToday = $row['total'];
            break;

        case 1:
            $totalIssuesClosedToday = $row['total'];
            break;
    }
}

和错误...

Fatal error: Uncaught exception 'Zend_Db_Statement_Exception' with message
'SQLSTATE[42S22]: Column not found: 1054 Unknown column 'Y' in 'where clause''
in C:\xampp\htdocs\dating\trunk\library\Zend\Db\Statement\Pdo.php:238

Stack trace:
#0 C:\xampp\htdocs\dating\trunk\library\Zend\Db\Statement.php(283): Zend_Db_Statement_Pdo->_execute(Array)
#1 C:\xampp\htdocs\dating\trunk\library\Zend\Db\Adapter\Abstract.php(484): Zend_Db_Statement->execute(Array)
#2 C:\xampp\htdocs\dating\trunk\library\Zend\Db\Adapter\Pdo\Abstract.php(235): Zend_Db_Adapter_Abstract->query('SELECT status, ...', Array)
#3 C:\xampp\htdocs\dating\trunk\html\siteadmin.php(59): Zend_Db_Adapter_Pdo_Abstract->query('SELECT status, ...')
#4 {main} thrown in C:\xampp\htdocs\dating\trunk\library\Zend\Db\Statement\Pdo.php on line 238

My sql statement is not working with Zend, its complaining about the Count(*) field... what am I doing wrong?

// get open/closed
$stmt = $db->query('SELECT status, count(*) as total FROM reported_issues WHERE date_reported >= '.$today.' AND status IN (0,1) GROUP BY status');
while ($row = $stmt->fetch())
{
    switch ($row['status'])
    {
        case 0:
            $totalIssuesToday = $row['total'];
            break;

        case 1:
            $totalIssuesClosedToday = $row['total'];
            break;
    }
}

and the errors...

Fatal error: Uncaught exception 'Zend_Db_Statement_Exception' with message
'SQLSTATE[42S22]: Column not found: 1054 Unknown column 'Y' in 'where clause''
in C:\xampp\htdocs\dating\trunk\library\Zend\Db\Statement\Pdo.php:238

Stack trace:
#0 C:\xampp\htdocs\dating\trunk\library\Zend\Db\Statement.php(283): Zend_Db_Statement_Pdo->_execute(Array)
#1 C:\xampp\htdocs\dating\trunk\library\Zend\Db\Adapter\Abstract.php(484): Zend_Db_Statement->execute(Array)
#2 C:\xampp\htdocs\dating\trunk\library\Zend\Db\Adapter\Pdo\Abstract.php(235): Zend_Db_Adapter_Abstract->query('SELECT status, ...', Array)
#3 C:\xampp\htdocs\dating\trunk\html\siteadmin.php(59): Zend_Db_Adapter_Pdo_Abstract->query('SELECT status, ...')
#4 {main} thrown in C:\xampp\htdocs\dating\trunk\library\Zend\Db\Statement\Pdo.php on line 238

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

空宴 2024-07-29 09:30:03

除了其他人注意到的关于 $today 中的值之外,您确实应该在查询中使用绑定参数

<?php

$stmt = $db->query(
    "SELECT status, count(*) as total
       FROM reported_issues
      WHERE date_reported >= ?
        AND status IN (0,1)
      GROUP BY status"
    ,array( $today )
);

Aside from what others have noted about the value you have in $today, you really should be using bound parameters with your queries

<?php

$stmt = $db->query(
    "SELECT status, count(*) as total
       FROM reported_issues
      WHERE date_reported >= ?
        AND status IN (0,1)
      GROUP BY status"
    ,array( $today )
);
熊抱啵儿 2024-07-29 09:30:03

投诉实际上是关于 WHERE - 看起来您的 $today 变量不包含您认为应该包含的内容。 具体来说,因为它说:

'SQLSTATE[42S22]: Column not found: 1054 Unknown column 'Y' in 'where clause''

看来您的变量包含不应该存在的 Ycount 似乎与该问题无关。

The complaint is actually about the WHERE -- looks like your $today variable does not contain what you think it should. Specifically, since it says:

'SQLSTATE[42S22]: Column not found: 1054 Unknown column 'Y' in 'where clause''

it seems your variable contains a Y that shouldn't be there. The count appears to have nothing to do with the issue.

我最亲爱的 2024-07-29 09:30:03

我猜你的 $today 变量已损坏。 将第一行(用于测试)

$stmt = $db->query('SELECT status, count(*) as total FROM reported_issues WHERE date_reported >= '.$today.' AND status IN (0,1) GROUP BY status');

更改为:

$sql='SELECT status, count(*) as total FROM reported_issues WHERE date_reported >= '.$today.' AND status IN (0,1) GROUP BY status';
try{
   $stmt = $db->query($sql);
} catch (Exception $e) {
   echo $sql."\n";
   throw $e;
}

然后您可以看到它尝试执行的原始 SQL。

I'm guessing your $today variable is corrupt. Change the first line (for testing)

from

$stmt = $db->query('SELECT status, count(*) as total FROM reported_issues WHERE date_reported >= '.$today.' AND status IN (0,1) GROUP BY status');

to:

$sql='SELECT status, count(*) as total FROM reported_issues WHERE date_reported >= '.$today.' AND status IN (0,1) GROUP BY status';
try{
   $stmt = $db->query($sql);
} catch (Exception $e) {
   echo $sql."\n";
   throw $e;
}

Then you can see the raw SQL it's trying to execute.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文