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

Why 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(); }

<!DOCTYPE html>

    <?php
    $pageTitle="feedback";
     include ("inc/header.php");
 ?>

<?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"); ?>

2 Answers

Try changing your bindParams to PDO::PARAM_STR perhaps for any values that are non-integers.

not working

Try changing your query to "SELECT id, sub FROM students…" (add a space after the first comma).

nop 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(); }

Is 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.