我想从mysql数据库中获取时间列并与当前时间进行比较。只有当它与当前时间匹配时才应该执行。我是php的新手。 This is my Table
我尝试的是
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$today = date("h:i a")
$sql = "SELECT * FROM Table_name WHERE time = $today";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// execute you code
} else {
}
$conn->close();
?>
但问题出现在我的表连字符和&#39;或&#39;符号存在,我不知道如何比较? 是否可以像这种格式一样比较时间?
时间列的数据类型是longtext
答案 0 :(得分:0)
为此,您只需更改数据库中存储时间的方式即可。将时间存储在UNIX TIMESTAMP中。我使用了你的代码。
要插入UNIX TIMESTAMP的mysql
$sql = "INSERT into `Table_name` set time = time()";
您的代码经过修改 $ today = time();
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$today = time();
$sql = "SELECT * FROM Table_name WHERE time = '".$today."'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// execute you code
} else {
}
$conn->close();
?>