所以我有一个phpMyAdmin数据库,我有一个名为Events
的数据库,在那里我有一个名为Event_table
的表,我有一个名为date
的字段,有一个DATE函数用于存储事件的日期。我想将PHP的当前日期与数据库date
进行比较。例如。今天是2017-06-23,它存储在数据库中,现在我想将这个日期与PHP的日期进行比较,如果它们彼此匹配,那么PHP应该回应"事件今天",如果它没有匹配那么它应该回应"没有事件"。
有人可以给我完整编辑的代码。
<?php
define ('DB_User','root');
define ('DB_Password','password');
define ('DB_HOST','localhost');
define ('DB_Name','events');
$con = mysqli_connect(DB_HOST, DB_User, DB_Password, DB_Name);
if(!$con){
die('Error Connecting');
}
//Don't this need remove at the end of coding...
echo "Connected successfully";
$date = DATE('y-m-d', strtotime("now"));
$sql = "SELECT date FROM Event_table WHERE date = date(now())";
$Query = mysql_query($sql);
$Row = mysql_fetch_array($Query, MYSQL_ASSOC);
$Compare = $Row[0];
if ($Compare == DATE('y-m-d', strtotime("now"))) {
echo "Yes";
}
else {
echo "No";
}
?>
答案 0 :(得分:0)
你走了!
您应该将日期传递给您的查询,然后检查是否有任何结果。你应该以不同的方式获得你的$ date变量。见下文。
<?php
# ...
$date = (new \Date())->format('Y-m-d');
$sql = "SELECT * FROM Event_table WHERE date = '$date'";
$Query = mysql_query($sql);
$Row = mysql_fetch_array($Query, MYSQL_ASSOC);
if (count($Row)) {
echo "Yes";
}
else {
echo "No";
}
?>
答案 1 :(得分:0)
所以最后我想出来所以这里是代码,如果有人需要它。
<?php
$servername = "localhost";
$username = "root";
$password = "password";
$dbname = "event";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
date_default_timezone_set("Europe/London");
$compare = date("Y-m-d");
/*echo $compare;*/
$sql = "SELECT date FROM event_table WHERE date = '$compare'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "Date: " . $row["date"]. "<br>";
echo '<img src="C:\Apache24\htdocs\Teying\yes_logo.png alt="icon" />';
}
} else {
echo "0 results";
}
$conn->close();
?>