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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?