如何从月份为当前月的列中的SQL日期中选择月份 [英] How to select month from sql date in column where month is currentmonth
问题描述
我需要做出一条选择语句,从客户WHERE返回Voornaam,Tussenvoegsel,Achternaam(我的数据库中有一列名为Geboortedatum,在这里我有一个日期.我需要上面命名的值,其中Geboortedatum是与$currentmonth
相同.我已经有一个返回currentmonth
的json函数.)
需要明确说明的是:我需要Voornaam,Tussenvoegsel和Achternaam,其中来自表客户的Geboortedatum(DD-MM-YYYY)月与$currentmonth
相同.
I need to make a select statement that returns Voornaam, Tussenvoegsel, Achternaam from customer WHERE (I have a column in my database which is called Geboortedatum, in here I have a date. I need al the values named above where Geboortedatum is the same as $currentmonth
. I already have a json function which returns the currentmonth
.) I am stuck with what to put after WHERE.
Just to be clear: I need Voornaam, Tussenvoegsel and Achternaam where the month of Geboortedatum(DD-MM-YYYY) from my table customer is the same as $currentmonth
.
$conn = new mysqli("localhost", "root", "Habt2002", "fca");
if ($_POST['key'] == 'bijnaJarig') {
$currentmonth = $conn->real_escape_string($_POST['currentmonth']);
$sql = $conn->query("SELECT Voornaam, Tussenvoegsel, Achternaam, Telefoonnummer, Email
FROM customer
WHERE ??????? ");
$data = $sql->fetch_array();
$jsonArray = array(
'voornaam' => $data['Voornaam'],
'tussenvoegsel' => $data['Tussenvoegsel'],
'achternaam' => $data['Achternaam'],
'telefoonnummer' => $data['Telefoonnummer'],
'emailbijnajarig' => $data['Email']
);
exit(json_encode($jsonArray));
}
推荐答案
使用MONTH()
和CURDATE()
MySQL函数,您可以执行
Using the MONTH()
and CURDATE()
MySQL functions you can do
WHERE MONTH(Geboortedatum) = MONTH(CURDATE())
您可能还想添加YEAR()
支票
AND YEAR(Geboortedatum) = YEAR(CURDATE())
除非您希望获得本月中所有年份的所有数据
unless you want all data for this month over multiple years
您仅从结果集中获取一行,您要么需要遍历结果集中的n
行,要么使用fetch_all()
方法.在这种情况下,fetch_all()
似乎是最简单的方法.
You are only fetching ONE row from the resultset, you either need to loop over the n
rows in the resultset OR use the fetch_all()
method. In this case the fetch_all()
seems like the simplest approach.
$conn = new mysqli("localhost", "root", "xxxx", "fca");
if (!$conn) {
echo "Error: Unable to connect to MySQL." . PHP_EOL;
echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL;
echo "Debugging error: " . mysqli_connect_error() . PHP_EOL;
exit;
}
if ($_POST['key'] == 'bijnaJarig') {
$sql = $conn->query("SELECT Voornaam, Tussenvoegsel, Achternaam, Telefoonnummer, Email
FROM customer
WHERE MONTH(Geboortedatum) = MONTH(CURDATE())");
$all_rows= $sql->fetch_all();
echo json_encode($all_rows);
}
这篇关于如何从月份为当前月的列中的SQL日期中选择月份的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!