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 trialFurquan Ahmad
5,148 PointsMy SQL query only returns the first result
I want to select multiple statements, but the SQL query only returns the results from the first condition where subject 1 = subject and grade 1 = grade 1
<?php
$stmt = $db->prepare("SELECT * FROM paststudent WHERE subject1 =:subject1 AND grade1=:grade1 AND :subject2=:subject2 AND :grade2 =:grade2");
$stmt->execute(array(':subject1' => $subject1, ':grade1' => $grade1, ':subject2' => $subject2, ':grade2' => $grade2));
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
?>
1 Answer
Andrew Shook
31,709 PointsYou have a syntax error in your select statement. You need to remove the colon from in front of the first subject 2 and grade2.
<?php
// change this
$stmt = $db->prepare("SELECT * FROM paststudent WHERE subject1 =:subject1 AND grade1=:grade1 AND :subject2=:subject2 AND :grade2 =:grade2");
//to this
$stmt = $db->prepare("SELECT * FROM paststudent WHERE subject1 =:subject1 AND grade1=:grade1 AND subject2=:subject2 AND grade2 =:grade2");