1

I have the following php select sql statement, which I can not get it to work. Please help:

$sql = "SELECT student_email, student_id, grades_id FROM student_table WHERE email_alert = 1 AND class_grade >= '$var' AND student_id in (
  select student_id
  from student_alerts
  where CURRENT_TIMESTAMP < student_grad_date)" ;

In this SQL $var = 85, student_id is a primary key in student_table and a foreign key in the student_alerts table. Also student_id is referenced to the primary key in the student_table. I believe the error might be somewhere around CURRENT_TIMESTAMP, the goal of which is to compare if today's date is not past the student's graduation date or it might be something in the syntax that I can not catch.

Thank you!

6
  • 1
    what error you are getting? Commented Jan 5, 2018 at 5:53
  • 2
    Is there any error? try to run that code in sql and change the value of var to specific value and see if it do what it does or if an error occurs.. Commented Jan 5, 2018 at 5:54
  • @SureshKamrushi . From the logs i get "mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given", which i know is a sql error. Commented Jan 5, 2018 at 6:16
  • @Shadow - Thank you for the tip. I did run the SQL on the DB and it runs successfully. As you suggested the var is the issue. Replacing it with a actual value runs the query. Echo-ing the var gives me the same value, but it doesn't seem to work with or without the parenthesis. Commented Jan 5, 2018 at 6:22
  • I'll speculate again.. Try AND class_grade >= ".$var.".. If its not working maybe the connection or other factors, also another tip try to learn about Prepared Statements and why is it necessary... Commented Jan 5, 2018 at 6:27

2 Answers 2

1

You are using CURRENT_TIMESTAMP which provides a timestamp and student_grad_date is just a date.

You can use CURDATE() instead, which also provide you just the date

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for your suggestion.
0

Gotta love PHP. After the tips from @Shadow Fiend, i did some research and the correct syntax that worked was:

$sql = "SELECT student_email, student_id, grades_id FROM student_table WHERE email_alert = 1 AND class_grade >= '". mysql_real_escape_string($var) ."' AND student_id in ( select student_id from student_alerts where CURRENT_TIMESTAMP < student_grad_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.