dongpao5127 2016-08-01 16:26
浏览 41
已采纳

比较使用curdate而忽略Year? - mysqli,PHP?

I am trying to check for Birthdays from data in the table using CURDATE() function. Sadly My code currently only outputs names if the birthDate is the current date! (i.e inluding year). I am trying to figure out how to ignore Year here and only compare month and day.

Please assume this is the table

| 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  |

This is the code

<?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>";
                            }
                    }   

            }
        }   
}

>?

This code only outputs

EFGH
123456789  
LMNO
123456789 

while it should have been all except book_no -3. Please Help

  • 写回答

1条回答 默认 最新

  • dph58509 2016-08-01 16:46
    关注

    Use the MONTH and DAY functions provided in MySQL to compare just those parts of the date to the date you have stored in the table.

    $sql2 = "SELECT name,mobile_no  FROM " .$var1. " WHERE MONTH(dob) = " . date('m') . " AND DAY(dob) = " . date('d');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 易康econgnition精度验证
  • ¥15 线程问题判断多次进入
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致