0

I have a mysql database. I have a php script, that connects to my database. My database consists of 3 tables, error, date, email address, as per the screenshot

Database structure

my php query is as follows:

<?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);
?>

I would like to replace '2013-01-28' with yesterdays date and in that format Y-m-d. How can I do this?

Any feedback would be appreciated

1
  • Depending on your script you must choose either to use PHP generated date or MYSQL generated.. If you insert date generated from your PHP then generate date string in PHP and use it else you should use mysql date_sub function to get the correct date :) Commented Apr 26, 2013 at 13:06

6 Answers 6

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());
Sign up to request clarification or add additional context in comments.

1 Comment

No probs, happy to help, if your happy with it please make sure to mark it as the answer :)
3

Why not use the MySQL function? They are meant to be used in MySQL.

SELECT * FROM microcompany WHERE date=DATE(DATE_SUB(NOW(), INTERVAL 1 DAY)) AND code='2.0.0'

btw. mysql-* functions are deprecated: http://www.php.net/manual/en/function.mysql-query.php

3 Comments

Thank you for the feed back, I tried that but for some reason does not work on my server.
Pardon me, I forgot to extract only the date from the function, see my updated answer.
No problem, glad you fixed it! :)
1
$date = new DateTime('Yesterday');
echo $date->format('Y-m-d');

Or

$date = new DateTime('2013-01-28');
$date->modify('-1 day');
echo $date->format('Y-m-d');

FYI, you shouldn't use mysql_* functions in new code. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO, or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial.

Comments

0

use native mysql functions for that purpose.

$result = mysql_query("SELECT * FROM microcompany WHERE
date=date_sub(date(now()), interval 1 day) AND code='2.0.0'")
or die(mysql_error());

Comments

0
$date = new DateTime('Yesterday');
$fdate = $date->format('Y-m-d');
echo $fdate;

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.