Php 在mysql中查询日期不返回任何内容?
我试图查询一个具有如下值的数据库表Php 在mysql中查询日期不返回任何内容?,php,mysql,timestamp,Php,Mysql,Timestamp,我试图查询一个具有如下值的数据库表 id = 1 tablename = 1 (bigint) timestampstart = 2011-08-06 13:54:17 (timestamp) timestampend = 2011-08-06 14:54:17 (timestamp) 因此,我使用此函数尝试返回数据库名称,我提供的时间戳介于开始时间戳和结束时间戳之间 function getDatabase($timestamp){ $link = new MySQLi($host,
id = 1
tablename = 1 (bigint)
timestampstart = 2011-08-06 13:54:17 (timestamp)
timestampend = 2011-08-06 14:54:17 (timestamp)
因此,我使用此函数尝试返回数据库名称,我提供的时间戳介于开始时间戳和结束时间戳之间
function getDatabase($timestamp){
$link = new MySQLi($host, $root, $password, $database);
$name = '';
$result = $link->query("SELECT `tablename` FROM `datas` WHERE `timestampstart` =< '$timestamp' AND `timestampend` >= '$timestamp'");
if($link->error){
return $link->error;
}
if($result->num_rows > 0){
while($row = $result->fetch_assoc()){
$name .= $row['tablename'];
}
return $name;
}else{
return 'no table';
}
}
我得到的只是“没有桌子”的回复
顶部只有一行我定义了表的结构,非常感谢您的帮助。在
选择查询中使用UNIX\u TIMESTAMP()
函数比较时间戳。您的查询应如下所示:
SELECT `tablename`
FROM `datas`
WHERE UNIX_TIMESTAMP(timestampstart) <= UNIX_TIMESTAMP('$timestamp')
AND UNIX_TIMESTAMP(timestampend) >= UNIX_TIMESTAMP('$timestamp')
除此之外,我还可以看到范围问题,因为$host
、$root
、$password
和$database
在您的函数范围内不可用。使用global
或将数据库凭据作为函数参数传递给您的getDatabase()
函数。请参阅您的SELECT
查询,=我仍然得到‘no table’=您的数据库中是否有数据表?是的,数据在表中,我是如何在上面显示的?您有任何错误吗?我对变量使用了全局关键字,并且它起了作用,谢谢您我在我还是新手之前从未听说过这一点php@JRowan很高兴我能帮忙。快乐编码!:-)
SELECT `tablename`
FROM `datas`
WHERE UNIX_TIMESTAMP(timestampstart) <= UNIX_TIMESTAMP('$timestamp')
AND UNIX_TIMESTAMP(timestampend) >= UNIX_TIMESTAMP('$timestamp')
SELECT `tablename`
FROM `datas`
WHERE UNIX_TIMESTAMP('$timestamp') BETWEEN UNIX_TIMESTAMP(timestampstart) AND UNIX_TIMESTAMP(timestampend)