0

I have been trying to get PHP to retrieve any results that have the date stamp that is the same as in the column 'date'.
It then loops round and prints out all the data retrieved.
The actual retrieval works fine, its just my condition!

Code:

$currentdate = date('Y/m/d'); 
$echo . $currentdate;
$query = "SELECT * FROM `pupils` WHERE `$currentdate` = date";//Grab the data
$result = mysqli_query($con, $query);

Any help greatly appreciated!

3
  • 1
    MySQL has a CURDATE() function... Commented Jan 28, 2015 at 20:16
  • Your where clause is backwards Commented Jan 28, 2015 at 20:18
  • 1
    Sidenote: $echo errrr, that's just echo ;-) echo $currentdate; and no dot required. Commented Jan 28, 2015 at 20:22

3 Answers 3

1

proper use of the WHERE clause looks like this.

SELECT column_name,column_name
FROM table_name
WHERE column_name operator value;

your value is date. what is date? I'll assume that date is a column name here. Something that would work would like this.

$query = "SELECT * FROM `pupils` WHERE `date` = $currentdate";
Sign up to request clarification or add additional context in comments.

Comments

0

You're using magic quotes around the date value where you ought to be using single quotes. Magic quotes cause MySQL to intrepret the date value (being passed in as Y/m/d as an identifier (like a column name). See revised code below with comments.

$currentdate = date('Y/m/d'); 
$echo . $currentdate;
// Magic quotes (`) are used to denote identifier names in MySQL
$query = "SELECT * FROM `pupils` WHERE '$currentdate' = date";
$result = mysqli_query($con, $query);

You could offload the whole process to the query itself, MySQL offers a CURRENT_TIMESTAMP function that will give you the current server time, you can further manipulate it to get the specificity that you would like.

1 Comment

Cheers! I won't offload it for now but will certainly look into that!
0

replace the backtics arround $currentdate with single qoutes and add the backticks around date, because date is a reserved word in mysql.

$query = "SELECT * FROM `pupils` WHERE '$currentdate' = `date`";//

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.