-1

My sql, keeps on producing an error with an empty result. I'm trying to select a past student from the database that has got the same grades and results as a past student stored in the database. I'm using pdo statements to prepare the statements.

I keep on getting returned empty queries, even though in the database there is a student that has taken maths (subject 1) and got an A (grade1). This query won't work.

$query = $db->prepare("SELECT * FROM paststudent WHERE subject1 = :subject1 AND grade1 = :grade1");

.

if ($autumn != "" and $winter == "" and $spring == "" and $summer == "") {
       $query = $db->prepare("SELECT * FROM paststudent WHERE subject1 = :subject1 AND grade1 = :grade1 AND subject2 = :subject2 AND grade2 = :grade2 AND subject3 = :subject3 AND grade3 = :grade3 AND subject4 = :subject4 AND grade4 = :grade4 AND autumn = :autumn");
        // binds the value to the query variable 
        $query->bindValue(':subject1', $subject1, PDO::PARAM_STR);
        $query->bindValue(':grade1', $grade1, PDO::PARAM_STR);
        $query->bindValue(':subject2', $subject2, PDO::PARAM_STR);
        $query->bindValue(':grade2', $grade2, PDO::PARAM_STR);
        $query->bindValue(':subject3', $subject3, PDO::PARAM_STR);
        $query->bindValue(':grade3', $grade3, PDO::PARAM_STR);
        $query->bindValue(':subject4', $subject4, PDO::PARAM_STR);
        $query->bindValue(':grade4', $grade4, PDO::PARAM_STR);
        $query->bindValue(':autumn', $autumn, PDO::PARAM_STR);
        // executes the query
        $query->execute();
        // the value of the query is stored to result
        $result = $query->fetch(PDO::FETCH_ASSOC);
    // fetch() instead of fetchAll just gets the first result



    }
jeroen
  • 91,079
  • 21
  • 114
  • 132

1 Answers1

0

can you please dump your sql query here, that will be easy to solve your problem. what i can see that all the AND condition and if any of AND condition false then you will get empty result.

Zaib Khan
  • 470
  • 4
  • 7
  • $query = $db->prepare("SELECT * FROM paststudent WHERE subject1 = :subject1 AND grade1 = :grade1 AND subject2 = :subject2 AND grade2 = :grade2 AND subject3 = :subject3 AND grade3 = :grade3 AND subject4 = :subject4 AND grade4 = :grade4 AND autumn = :autumn"); // binds the value to the query variable – furquan ahmad Apr 20 '15 at 13:46