0
try {
  $db = new PDO("mysql:host=localhost;dbname=placement", "rot", "password");
} catch (Exception $e) {
    die("An error occured" . $e->getMessage());
}
$username = $_POST['username'];
$password = $_POST['password'];
$stmt = $db->prepare("SELECT regno,password FROM placement.authenticate 
                    WHERE regno = ? AND password = ? LIMIT 1");
$result = $stmt->execute(array( $username, $password ));
if ($result) {
  $row = $stmt->fetch(PDO::FETCH_ASSOC);
  echo "{$row['admin']}<br/>";
} else {
  die("Error Occurred<br/>");
}

I`m using mysql.

First Problem

I'm using the above code to check my database for the username, password, if($result) I understand it return true if the $stmt is executed but is there anyway I can check if the query has any result?

I read one of the Q/A here at stackoverflow which said that use of $stmt->rowCount() does not necessarily return the desired result is there any other Simple workaround to this one of comments suggested use of if($stmt->fetch()>0) is this approach correct?

Second Problem

I fetch the current row using $row=$stmt->fetch(PDO::FETCH_ASSOC); I get error saying undefined index.at Line the

echo "{$row['admin']}";

Also when I use print_r($row) I get only 2 columns not the 3rd.

Sorry for the dumb question but is this result of any of the PDO statments I have used earlier in the code.(I`m learning php)

I have gone through these Links Link 1 Link 2

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
AAB
  • 1,594
  • 2
  • 25
  • 40

2 Answers2

3

You're not actually selecting that value in your query. You're only selecting regno and password.

$stmt=$db->prepare("SELECT regno,password FROM placement.authenticate 
            WHERE regno = ? AND password = ? LIMIT 1");       

needs to become:

$stmt=$db->prepare("SELECT regno,password,admin FROM placement.authenticate 
            WHERE regno = ? AND password = ? LIMIT 1");       
John Conde
  • 217,595
  • 99
  • 455
  • 496
  • Thank You I guess I should do a bit more reading about Queries – AAB Dec 23 '13 at 14:37
  • regarding problem 1 what should I do? – AAB Dec 23 '13 at 14:39
  • `$stmt->rowCount() ` should work for you. If it doesn't you should post a new question showing the code you're using including that statement and an good example of when it is failing and what the results are when it does. That will help us troubleshoot it. – John Conde Dec 23 '13 at 14:41
  • Hi,$stmt->rowCount() does work for me but I read about it not being reliable for other DB in Q/A so I thought its use may not be correct.I want your opinion on something do you think I should stop searching the net and find the various methods of PDO.Instead read it and about queries(i did read guess content did not pass to my brain well :*( ) – AAB Dec 23 '13 at 14:45
  • If it suits your purposes that's all you need to know. Don't worry about the other stuff until it actually affects your project. – John Conde Dec 23 '13 at 14:47
  • Thank You :) then I will use rowCount – AAB Dec 23 '13 at 14:49
2

Why would you expect to get 3 columns from your query?

SELECT regno,password FROM placement.authenticate etc...
       ^^^^^^^^^^^^^^

You're only ever fetching those two specified columns. There is no admin result in your $row result because you didn't tell the DB to fetch it. e.g. you need

SELECT regno,password,admin etc...
                     ^^^^^^---you need this
Marc B
  • 356,200
  • 43
  • 426
  • 500