Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/241.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/59.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在mysql中查询日期不返回任何内容?_Php_Mysql_Timestamp - Fatal编程技术网

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)