Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

PHP SQL select query not working

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.

<?php

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

This is what i'm trying to achieve with a big SQL query, trying to select loads of variables from the past student database.

<?php

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



    }

I also var_dumped the query, and the query returned NULL. Not sure what i'm doing wrong

2 Answers

If you dumped out $qry and it returned null, then it sounds like $db->prepare() is failing,

Is $db set properly?

Yes my program runs at www.grademanager.16mb.com/ the login works fine which uses the same connection file, so it can't be towards the db not set properly

Have you made double sure that it's set before you try to use it here?

Wait wait.. when you say your query is null, do you mean the overall results or $db->prepare ?