我有一个mysql数据库。 我有一个PHP脚本,连接到我的数据库。我的数据库包含3个表,错误,日期,电子邮件地址,根据屏幕截图
我的php查询如下:
<?php
mysql_connect("localhost", "username", "password") or die(mysql_error());
mysql_select_db("pmta_reporting") or die(mysql_error());
$result = mysql_query("SELECT * FROM microcompany WHERE date='2013-01-28' AND code='2.0.0'") or die(mysql_error());
$count= mysql_num_rows($result);
echo $count;
mysqli_close($con);
?>
我想用昨天的日期和格式Y-m-d替换'2013-01-28'。 我怎么能这样做?
任何反馈都将不胜感激
答案 0 :(得分:4)
$yesterday = date("Y-m-d", strtotime('-1 day'));
$result = mysql_query("SELECT * FROM microcompany WHERE date='$yesterday' AND code='2.0.0'") or die(mysql_error());
答案 1 :(得分:3)
为什么不使用MySQL功能?它们应该用在MySQL中。
SELECT * FROM microcompany WHERE date=DATE(DATE_SUB(NOW(), INTERVAL 1 DAY)) AND code='2.0.0'
顺便说一句。不推荐使用mysql- *函数:http://www.php.net/manual/en/function.mysql-query.php
答案 2 :(得分:1)
$date = new DateTime('Yesterday');
echo $date->format('Y-m-d');
或者
$date = new DateTime('2013-01-28');
$date->modify('-1 day');
echo $date->format('Y-m-d');
仅供参考,you shouldn't use mysql_*
functions in new code。它们不再被维护and are officially deprecated。请参阅red box?转而了解prepared statements,并使用PDO或MySQLi - this article将帮助您确定哪个。如果您选择PDO here is a good tutorial。
答案 3 :(得分:0)
$date = new DateTime('Yesterday');
$fdate = $date->format('Y-m-d');
echo $fdate;
答案 4 :(得分:0)
为此目的使用本机mysql函数。
$result = mysql_query("SELECT * FROM microcompany WHERE
date=date_sub(date(now()), interval 1 day) AND code='2.0.0'")
or die(mysql_error());
答案 5 :(得分:-1)
<?php
$yesterday = date('Y-m-d', strtotime('-1 day',strtotime(date("y-m-d")) ));
$query = "SELECT * FROM microcompany WHERE date='".$yesterday."' AND code='2.0.0'";
$newQuery = 'update microcompany set date = "'.$yesterday.' where date = "'.$oldDate.'" and code ="2.0.0"';
?>