我正在尝试使用CURDATE()函数从表中的数据检查生日。可悲的是,如果birthDate是当前日期,我的代码目前只输出名称! (即包括年份)。我想弄清楚如何忽略年份,只比较月和日。
请假设这是表格
| book_no | name | dob | mobile_no |
|---------|-----------|------------|------------|
| 1 | ABCD | 1995-08-01 | 123456789 |
| 2 | EFGH | 2016-08-01 | 123456789 |
| 3 | HIKJ | 2016-05-01 | 123456789 |
| 4 | LMNO | 2016-08-01 | 123456789 |
| 5 | PQRS | 1995-08-01 | 123456789 |
这是代码
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "gold";
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn)
{
die("Connection failed: " . mysqli_connect_error());
}
$sql1 = "SHOW TABLES";
$result1 = mysqli_query($conn,$sql1);
if(!$result1)
{
echo "Error fetching data from tempTable: " . mysqli_error($conn);
}
else
{
while ($row1 = $result1->fetch_assoc())
{
foreach($row1 as $key1 => $var1)
{
$sql2 = "SELECT name,mobile_no FROM " .$var1. " WHERE dob = CURDATE()";
$result2 = mysqli_query($conn,$sql2);
$bBoy = array();
if(!$result2)
{
echo "Error fetching data from tempTable: " . mysqli_error($conn);
}
else
{
while ($row2 = $result2->fetch_assoc())
{
echo $row2['name']."<br>";
echo $row2['mobile_no']."<br>";
}
}
}
}
}
>?
此代码仅输出
EFGH
123456789
LMNO
123456789
虽然它应该是除book_no -3以外的全部。 请帮助
答案 0 :(得分:1)
使用MySQL中提供的MONTH和DAY函数将日期的那些部分与您在表格中存储的日期进行比较。
$sql2 = "SELECT name,mobile_no FROM " .$var1. " WHERE MONTH(dob) = " . date('m') . " AND DAY(dob) = " . date('d');