尝试将数据库上的值与表单中传递的值匹配以检查用户是否存在时,我收到以下错误.
可捕获的致命错误:类PDOStatement的对象无法转换为字符串
这是我正在使用的代码:
//Check users login details
function match_login($username, $password){
//If the button has been clicked get the variables
try{
$dbh = new PDO("mysql:host=localhost;dbname=mjbox","root", "usbw");
} catch( PDOException $e ) {
echo $e->getMessage();
}
$stmt = $dbh->prepare("SELECT * FROM mjbox WHERE username=? AND password=?");
$stmt->bindParam(1, $username);
$stmt->bindParam(2, $password);
$stmt->execute();
$result = mysql_query($stmt);
if( mysql_num_rows($result) > 0 ){
echo 'There is a match!';
}else{
echo 'nooooo';
}
}
Run Code Online (Sandbox Code Playgroud)
mysql_query()和PDO不兼容,不能一起使用.您正在尝试传递mysql_query()期望字符串的PDO语句对象.相反,您希望$stmt通过PDO的一个提取方法获取行,或者检查返回的行数rowCount():
$stmt = $dbh->prepare("SELECT * FROM mjbox WHERE username=? AND password=?");
$stmt->bindParam(1, $username);
$stmt->bindParam(2, $password);
if ($stmt->execute()) {
// get the rowcount
$numrows = $stmt->rowCount();
if ($numrows > 0) {
// match
// Fetch rows
$rowset = $stmt->fetchAll();
}
else {
// no rows
}
}
Run Code Online (Sandbox Code Playgroud)