按时间戳字段中的日期筛选结果 [英] Filter results by date in timestamp field

查看:1028
本文介绍了按时间戳字段中的日期筛选结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经有一些帮助,但不知道为什么这不工作。

I have already had some help but not sure why this isn't working.

我试图使用表单让用户过滤他们的活动存储在数据库中)

I am trying to use a form to let a user filter their activity (which is stored in a DB)

我的代码:

$_GET['from'] = '01/11/2013';
$_GET['to']   = '25/11/2013';

$from = DateTime::createFromFormat('d/m/Y', $_GET['from']);
$to   = DateTime::createFromFormat('d/m/Y', $_GET['to']);

$sql = "
    SELECT * FROM transfer 
    WHERE personID = $user AND DATE(time) BETWEEN '%s' AND '%s'
";
$sql = sprintf($sql, $from->format('Y-m-d'), $to->format('Y-m-d'));

print_r($sql);

这将打印
SELECT * FROM transfer WHERE personID = 84587749 AND DATE(time)BETWEEN'2013-11-01'AND'2013-11-14'

当我在PHPmyadmin中查询时

When I query this in PHPmyadmin it shows the record, however not showing in my page?

推荐答案

SQL看起来不错,但是你似乎没有发出执行SQL查询在数据库中并检索结果??也许我缺少一些东西,但你需要连接到你的数据库:

The SQL looks fine but you don't appear to have issued the executed the SQL query in the database and retrieved the results?? Maybe I'm missing something but you need to connect to your database:

class DBi {
  public static $mysqli;
}

DBi::$mysqli = new mysqli('servername', 'database', 'password', 'user');

if (mysqli_connect_error()) {
  die('Connect Error (' . mysqli_connect_errno() . ') '
        . mysqli_connect_error());
}

然后您需要执行查询:

$result = DBi::$mysqli->query($sql) or die ("Unable to execute SQL command:".$sql);

最后,检索并使用结果:

And finally, retrieve and use the result:

$row = $result->fetch_assoc();
echo $row["fieldname"];

这篇关于按时间戳字段中的日期筛选结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆