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 Pointswhay i cant retrive the data ?
heres code i think error is in line "$results = $db->query("SELECT * FROM teacher WHERE name=$name AND password=$password ");"
<?php
$name='krishna';
$password='krishna';
try{
$db = new PDO("mysql:host=localhost;dbname=feedback;port=8808","root","krishna");
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$db->exec("SET NAMES 'utf8'");
}
catch (exception $e){
echo'Could Not Connect To The Database';
exit;
}
try{
$results = $db->query("SELECT * FROM teacher WHERE name=$name AND password=$password ");
} catch (Exception $ex) {
echo 'Data Can Not Be retrived';
exit();
}
var_dump($results->fetchall(PDO::FETCH_ASSOC));
?>
Matthew Bilz
15,829 PointsTry changing fetchall to fetchAll - could help.
krishna sapkal
Courses Plus Student 62 Pointsnop not workng
2 Answers
Matthew Bilz
15,829 PointsTry a prepared statement:
try{
$stmt = $db->prepare("SELECT * FROM teacher WHERE name = :name AND password = :password ");
$stmt->bindParam(':name', $name, PDO::PARAM_STR);
$stmt->bindParam(':password', $password, PDO::PARAM_STR);
$stmt->execute();
} catch (Exception $ex) {
echo 'Data Can Not Be retrived'; exit(); }
var_dump($stmt->fetchall(PDO::FETCH_ASSOC));
krishna sapkal
Courses Plus Student 62 Pointstysm
Matthew Bilz
15,829 PointsIf you're not getting anything in your var_dump, I might suggest checking your SQL query in your phpMyAdmin or another query program to make sure that the query itself is working.
You're getting the catch from your second try/catch block, then, of 'Data can not be retrieved?'
krishna sapkal
Courses Plus Student 62 PointsI am getting the result if i replace the veriable name ($name , $password) with string ('krishna' , 'krishna'). its jst not working because of variable.
Matthew Bilz
15,829 PointsMatthew Bilz
15,829 PointsSorry, if you refresh the page it should be there now.