2

我希望这个问题看起来并不含糊,但我正在将旧的 mysql 语句转换为 PDO 准备语句,并且我已经搜索但找不到答案。$count=mysql_num_rows($stmt);PDO中的等价物是什么?我已经包含了我的代码,所以你可以看到我在做什么,但基本上我正在为用户创建一个登录页面,我有一个 PDO 语句SELECT用于在数据库中搜索匹配的用户名和密码行。如果它想出一个,那么它将把$count变量增加到一个,这样我的IF陈述就成立了。

CHECK_LOGIN.PHP

<?php
require 'functions.php';
require 'DB.php';
ob_start();

// Define $myusername and $mypassword 
$myusername=$_POST['myusername']; 
$mypassword =$_POST['mypassword'];
$mypassword = md5($_POST['mypassword']);

checkLogin($conn,$myusername,$mypassword,$stmt);

// Mysql_num_row is counting table row
$count=mysql_num_rows($stmt);

// If result matched $myusername and $mypassword, table row must be 1 row
if($count==1){

// Register $myusername, $mypassword and redirect to file "login_success.php"
    session_start();
    $_SESSION['myusername'] = $myusername;
    session_is_registered("myusername");
    session_is_registered("mypassword"); 
    $myusername1 = $_SESSION['myusername'];
    //UPDATE LAST LOGIN IN DATABASE
    date_default_timezone_set('America/Chicago');
    $last_login = date('m/d/Y h:i:s a', time());
    lastLogin($conn,$myusername1,$last_login);
    header("location:form.php");


}
else {

echo "Wrong Username or Password";

}
ob_end_flush();
?>

FUNCTION.PHP 中的函数

function checkLogin($conn,$myusername,$mypassword,$stmt){

$myusername = stripslashes($myusername);
$myusername = mysql_real_escape_string($myusername);
$stmt = $conn->prepare('SELECT * FROM `CLL_users` WHERE `user_name`=:myusername AND `password`=:mypassword');
$stmt->execute(array(':myusername' => $myusername, ':mypassword' => $mypassword));
}
4

1 回答 1

4

从这里检查示例 #2:PDOStatement::rowCount

For most databases, PDOStatement::rowCount() does not return the number of rows affected by a SELECT statement. Instead, use PDO::query() to issue a SELECT COUNT(*) statement with the same predicates as your intended SELECT statement, then use PDOStatement::fetchColumn() to retrieve the number of rows that will be returned. Your application can then perform the correct action.

于 2012-11-04T02:29:59.237 回答