Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

PHP

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

Sorry, if you refresh the page it should be there now.

Try changing fetchall to fetchAll - could help.

nop not workng

2 Answers

Try 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));

tysm

If 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?'

I am getting the result if i replace the veriable name ($name , $password) with string ('krishna' , 'krishna'). its jst not working because of variable.