[英]PHP Select database rows from current month and display them with foreach()
I am making a payment system for my website, and i need a way to select everything from a database where the month and year is identical as the current month and year. 我正在为我的网站制作一个支付系统,我需要一种从月份和年份与当前月份和年份相同的数据库中选择所有内容的方法。 My date is formatted as dd/mm/yyy or 01-01-2010 for example.
例如,我的日期格式为dd / mm / yyy或01-01-2010。
After this is done, i need to display all results with a foreach() loop. 完成此操作后,我需要使用foreach()循环显示所有结果。 How could this be done?
怎么办呢? I've tried using the LIKE function in the mysql_query, but it just outputs an error.
我试过在mysql_query中使用LIKE函数,但它只会输出错误。
Here are some code i've come up with: 这是我想出的一些代码:
date_default_timezone_set('UTC');
$today = date("d-m-Y"); // Output something like 21-10-2014 for finding matching rows
$today_compare = substr($today, 2, 8);
$fetch_donate = mysql_query("SELECT * FROM `shop_payments` WHERE date LIKE '%today_compare%");
I will give you an answer, based on the assumption that you can change your date field in the database to actually be a date data type field. 我将基于您可以将数据库中的日期字段更改为实际为日期数据类型字段的假设为您提供答案。 I am also going to assume you change the column name to a non-reserved word
date
to something like payment_date
. 我还要假设您将列名更改为非保留字
date
更改为诸如payment_date
。 this is just a good habit to get into - not naming database objects the same as reserved words. 这只是进入的好习惯-不要将数据库对象的名称与保留字相同。
The query could be as simple as this: 查询可以像这样简单:
SELECT *
FROM `shop_payments`
WHERE `payment_date` LIKE CONCAT(YEAR(NOW()), '-', MONTH(NOW()), '%')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.