himmerz
himmerz

Reputation: 69

PHP MYSQL Display results where day is todays date

$today = CURDATE();
$result = mysqli_query($con,"SELECT * FROM Persons WHERE Day ='"$today"'");

The columns are: Name
Day
Time
Reg

And a select * from Persons works fine.

Upvotes: 0

Views: 631

Answers (3)

hizbul25
hizbul25

Reputation: 3849

$today = CURDATE();
$result = mysqli_query($con,"SELECT * FROM Persons WHERE Day LIKE '$today'");

Upvotes: 0

Gordon Linoff
Gordon Linoff

Reputation: 1269445

If day is a datetime, then use:

SELECT *
FROM Person
WHERE Day >= CURRENT_DATE and Day < CURRENT_DATE + interval 1 day;

The use of like for dates is bad practice, because it requires converting the date to a string. This prevents an index from being used.

If day has no time component, the above will work, but you can simplify it to:

SELECT *
FROM Person
WHERE Day = CURRENT_DATE;

You can also write:

SELECT *
FROM Person
WHERE date(Day) = CURRENT_DATE ;

(The parentheses on CURRENT_DATE are optional.)

Upvotes: 1

Mike Brant
Mike Brant

Reputation: 71384

You should be able to just specify your current date in the query (i.e. no need to calculate in PHP). This would also give you more consistant time handling in case web server and MySQL server have different timezones.

If Day is datetime or timestamp field use this:

SELECT * FROM Persons WHERE Day LIKE CONCAT(CURRENT_DATE(),'%')

If Day is date field use this:

SELECT * FROM Persons WHERE Day = CURRENT_DATE()

Upvotes: 2

Related Questions