dqnrk44682 2015-12-10 17:56
浏览 85
已采纳

PHP在特定错误上记录到error_log

I have a query in PHP that is like:

$result = $db->prepare("SELECT * FROM table WHERE page_url_match = :pageurlmatch AND datetime(post_time) > datetime('now', '-32 hours')"); # line 124
$result->bindValue(':pageurlmatch', $shorturl, SQLITE3_TEXT); # line 130

I keep getting an error:

[10-Dec-2015 12:20:09 America/New_York] PHP Warning: SQLite3::prepare(): Unable to prepare statement: 1, near "now": syntax error in XXX on line 124

[10-Dec-2015 12:20:09 America/New_York] PHP Fatal error: Call to a member function bindValue() on a non-object in XXX on line 130

How do I have PHP log a specific variable to the error_log file if this error shows up again. I know i can use error_log($var); but I do not know how to trigger this to only go if there is an error

  • 写回答

1条回答 默认 最新

  • dsfadsa08911 2015-12-10 18:04
    关注

    You start by stop assuming that DB calls will always succeed. In this case, you have an SQL syntax error, meaning the prepare call returned boolean false. You then used that boolean false as if it was a statement handle, and cause further problems.

    Never EVER assume success when dealing with external resources. Write defensively: assume failure, test for failure, and treat success as a pleasant surprise.

    Guessing that's PDO you're using, so either start testing for boolean false returns, or turn on exceptions and start try/catching everything.

    $stmt = $db->prepare(...);
    if ($stmt === false) {
       error_log('prepare failed for reason: ' . $db->errorInfo);
       die();
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog