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)