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 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题