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条)

报告相同问题?

悬赏问题

  • ¥30 Windows Server 2016利用兩張網卡處理兩個不同網絡
  • ¥15 Python中knn问题
  • ¥15 使用C#,asp.net读取Excel文件并保存到Oracle数据库
  • ¥15 C# datagridview 单元格显示进度及值
  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源