我想显示正在过生日的用户的用户名。
生日在birthdays
列中以DATE的形式存储在数据库中。
这是我目前的代码:
<?php
function birthday_today(){
$current_date = date('m-d');
$row = mysql_query("SELECT `username`, `birthday` FROM `users` WHERE MONTH(birthday) = MONTH(NOW()) AND DAY(birthday) = DAY(NOW())");
if (date('m-d', strtotime($row['birthday'])) == date('m-d')) {
return '<ul><li>' . implode('</li><li>', $row['username'], '</li></ul>');
}
}
echo birthday_today();
?>
然而,这似乎并没有回应任何事情。我已经尝试了几种不同的方法将数据放入回声中但到目前为止还没有成功。有没有人可以帮助我?
答案 0 :(得分:3)
您需要从结果中提取数组。
$users = array();
$row = mysql_query("SELECT `username`, `birthday` FROM `users` WHERE MONTH(birthday) = MONTH(NOW()) AND DAY(birthday) = DAY(NOW())");
while ($result = mysql_fetch_array($row)) {
if (date('m-d', strtotime($result['birthday'])) == date('m-d')) {
$users[] = $result['username'];
}
}
// output results
echo "<ul>";
foreach ($users as $user) {
echo "<li>".$user['username']."</li>";
}
echo "</ul>"
答案 1 :(得分:0)
这是另一种方法:
<?php
// just returns the users having a birthday
function birthday_today() {
$result = mysql_query("SELECT `username` FROM `users` WHERE MONTH(birthday) = MONTH(NOW()) AND DAY(birthday) = DAY(NOW())");
$users = array();
while ($row = mysql_fetch_assoc($result))
$users[] = $row['username'];
return $users;
}
// make a unordered-list out of the users having birthday
$ul = "<ul>";
foreach(birthday_today() as $username)
$ul .= "<li>$username</li>";
$ul .= "</ul>";
echo $ul;
?>