<?php
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']}
";
}
else{
die("Error Occured
");
}
?>
我正在使用mysql .
First Problem
我正在使用上面的代码来检查我的数据库中的用户名和密码if($ result)我不希望它返回true如果$ stmt被执行但是无论如何我可以检查查询是否有任何结果?我在这里读了一篇关于stackoverflow的Q / A,其中说 $stmt->rowCount() 的使用不一定是 return the desired result 还有其他任何简单的解决方法,这个评论建议使用if($ stmt-> fetch()> 0)这种方法正确?
Second Problem
我使用$ row = $ stmt-> fetch(PDO :: FETCH_ASSOC)获取当前行;我得到错误说未定义index.at排队
echo“{$ row ['admin']}”;
I use print_r($row) I get only 2 columns not the 3rd.
对于这个愚蠢的问题很抱歉,但这是我之前在代码中使用的任何PDO语句的结果 . (我正在学习php)
2 回答
为什么您希望从查询中获得3列?
您只需要获取这两个指定的列 . 您的
$row
结果中没有admin
结果,因为您没有告诉数据库获取它 . 例如你需要你're not actually selecting that value in your query. You'只选择
regno
和password
.需要成为: