我希望这个问题看起来并不含糊,但我正在将旧的 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));
}