dongqun5769 2018-04-17 16:31
浏览 154
已采纳

使用PHP和HTML输入搜索Sqlite数据库

EDIT - CODE BELOW SHOWS CORRECTIONS

I need to create a search function that will search a SQLite database for a certain date range.

For example:

  • if the user were to input "7", it would look for all records that have a date within the next 7 days.
  • If the user were to input "-7" it would look for all records in the last 7 days.

So far I have:

if (!empty($_POST)) {
    $sql = "SELECT * from table where record_date between date('now') and date('now', :record_date||' days')"; 
    $stmt = $db->prepare($sql);
    $stmt->bindValue(':record_date' , '%'.$webdata['record_date'].'%', SQLITE3_TEXT);
    $result = $stmt->execute();
    while ($row = $result->fetchArray()) { 
        echo '<p>' . htmlspecialchars($row['id'])  . " : " . 
        htmlspecialchars($row['record_date']).'</p>';
    }
}
?>

This doesn't seem to bring through any results.

If I use SqliteOnline.com to query the database this does work fine with the query:

SELECT * from table where record_date between date('now') and date('now', '7 days')
  • 写回答

2条回答 默认 最新

  • dousuohe5882 2018-04-18 13:16
    关注

    Named placeholders shouldn't be quoted as they are then interpreted as a literal value.

    Try changing:

    date('now', ':record_date days')
    

    to:

    date('now', :record_date||' days')
    

    You also need to modify the following from:

    $stmt->bindValue(':record_date' , '%'.$webdata['record_date'].'%', SQLITE3_TEXT);
    

    to:

    $stmt->bindValue(':record_date' , $webdata['record_date'], SQLITE3_TEXT);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 用python如何确定子元素在父元素中的位置
  • ¥15 obj文件滤除异常高程
  • ¥15 用mathematicas或者matlab计算三重积分
  • ¥15 Loop unrolling的runtime计算
  • ¥100 NVMe-oF的Target端,开启attr_offload后,测试失败。
  • ¥100 有偿照片马赛克去掉,保留原始数据
  • ¥15 c# winform http报错,如何解决?
  • ¥15 统计软件及应用-r软件
  • ¥15 爬虫返回的js数据结构如何处理
  • ¥15 python初学者中间insert部分不知道怎么写,请教各位中间如何填写。还想问问大家0基础如何入手python和c语言,期待各位的回复和帮助。