duanbinren8906 2014-10-01 13:05
浏览 174
已采纳

将FROM_UNIXTIME添加到WHERE子句时出现未知的PHP PDO错误

I have this code and I'm not sure why it isn't working. Apparently it is not catching any exceptions, I tried to debug and found out that it is breaking at the point where i add:

WHERE MONTH(FROM_UNIXTIME(unixtime))='7'

Because when I remove that, everything seem to work just fine.

<?php
try {
  $dbh = new PDO('mysql:host=localhost;dbname=myDb', 'root', 'password');
  $dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );

  $stmt = $dbh->prepare('SELECT col1, count(*) as frequency FROM myTable WHERE MONTH(FROM_UNIXTIME(unixtime))='7' GROUP BY col1 ORDER BY frequency DESC');
  $stmt->execute();

  // populate results
  $results = array();
  foreach ($stmt->fetchAll(PDO::FETCH_ASSOC) as $row) {
    $row_array['col1'] = $row['col1'];
    $row_array['frequency'] = $row['frequency'];

    array_push($results,$row_array);      
  }

  // and return to typeahead
  echo json_encode($results);           


  $dbh = null;
} catch (PDOException $e) {
  print "Error!: " . $e->getMessage() . "<br/>";
  die();    
}
?>

I would also appreciate any help about enabling error logs to show up, if any php.ini or other server configuration has to be edited or configured please let me know.

Thanks!

  • 写回答

2条回答 默认 最新

  • dongshi2836 2014-10-01 13:07
    关注

    You have a problem with quotations. do like this

    $stmt = $dbh->prepare("SELECT col1, count(*) as frequency 
     FROM myTable 
       WHERE MONTH(FROM_UNIXTIME(unixtime))='7' GROUP BY col1 ORDER BY frequency DESC");
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 有赏,i卡绘世画不出
  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码