user4803090
user4803090

Reputation: 59

Select id based on todays date

I got a table named stats_eu that stores id, acc_name and date. I would like to select id based on acc_name and date. The date in the database is stored like this: 2015-05-16 13:35:19 (datetime)

This is what I have tried that didn't work:

$result = mysql_query("SELECT `id` FROM `stats_eu` WHERE `acc_name`='".$name."' AND `date`='".date('Y-m-d')."'");
$row = mysql_fetch_array($result);

echo "name: " . $name . "<br />";
echo "id: " . $row['id'] . "<br />";

That code just prints the name but not the id. Could someone please point our my error?

Upvotes: 0

Views: 299

Answers (2)

Sabbir Ahmed Sourove
Sabbir Ahmed Sourove

Reputation: 515

MySql Date() function converts string or datetime into date.Just use this query

$result = mysql_query("SELECT `id` FROM `stats_eu` WHERE `acc_name`='".$name."' AND Date(`date`)=Date(NOW())" );

Upvotes: 1

user1844933
user1844933

Reputation: 3417

Try this....

$result = mysql_query("SELECT `id` FROM `stats_eu` WHERE `acc_name`='".$name."' AND `date` LIKE '".date('Y-m-d')."%'");

I added LIKE operator in your query...

Upvotes: 0

Related Questions