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 如何删除这个虚拟音频
  • ¥50 hyper默认的default switch
  • ¥15 网站打不开,提示502 Bad Gateway
  • ¥20 基于MATLAB的绝热压缩空气储能系统代码咨询
  • ¥15 R语言建立随机森林模型出现的问题
  • ¥20 unity内置语言切换的按钮设置
  • ¥15 中级微观经济学,生产可能性边界问题
  • ¥15 TCP传输时不同网卡传输用时差异过大
  • ¥15 请各位看看我写的属于什么算法,或者有更正确的写法?
  • ¥15 html5 qrcode 扫描器