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 trialkrishna sapkal
Courses Plus Student 62 PointsWhy Database query is returning null value?
heres code
try{ $results = $db->prepare("SELECT id,sub FROM student WHERE rollno = :rollno AND password = :password "); $results->bindParam(':rollno',$rollno,PDO::PARAM_INT); $results->bindParam(':password',$password, PDO::PARAM_INT) ; $results->execute(); } catch (Exception $ex) { echo 'Data Can Not Be retrived'; include ("inc/footer.php"); exit(); }
2 Answers
Matthew Bilz
15,829 PointsTry changing your bindParams to PDO::PARAM_STR perhaps for any values that are non-integers.
krishna sapkal
Courses Plus Student 62 Pointsnot working
Matthew Bilz
15,829 PointsTry changing your query to "SELECT id, sub FROM studentsβ¦" (add a space after the first comma).
krishna sapkal
Courses Plus Student 62 Pointsnop still not woking.....its woking i when inputs to the where clause are given as string..
ex try{ $results = $db->prepare("SELECT id , sub FROM student WHERE rollno = rollno AND password = password "); $results->bindParam(':rollno',$rollno,PDO::PARAM_STR); $results->bindParam(':password',$password, PDO::PARAM_STR) ; $results->execute(); }
Matthew Bilz
15,829 PointsIs ID a number/integer or a string? If it's an integer, that bindParam would be a PDO::PARAM_INT instead of the PDO::PARAM_STR like the password.
I'm just spitball troubleshooting here, hopefully it's something like this.
krishna sapkal
Courses Plus Student 62 Pointskrishna sapkal
Courses Plus Student 62 Points<!DOCTYPE html>
<?php $rollno = $_POST['user_rollno']; $password= $_POST['user_password']; include ('inc/db_connection.php'); try{ $results = $db->prepare("SELECT id,sub FROM student WHERE rollno = :rollno AND password = :password "); $results->bindParam(':rollno',$rollno,PDO::PARAM_STR); $results->bindParam(':password',$password, PDO::PARAM_STR) ; $results->execute(); } catch (Exception $ex) { echo 'Data Can Not Be retrived'; include ("inc/footer.php"); exit(); } $validater=$results->fetchall(PDO::FETCH_ASSOC);
if($validater==0){ Echo 'Invalid Rollno Or Password'; } else { var_dump($validater);
} ?> <?php include ("inc/footer.php"); ?>