[英]PDO prepared equivalent statement for mysql_num_rows
I hope this question does not seem vague but I am converting the old mysql statements to the PDO prepared statements and I have searched but cannot find the answer to this. 我希望这个问题似乎不模糊,但是我正在将旧的mysql语句转换为PDO准备的语句,并且已经搜索但找不到答案。 What is the equivalent of
$count=mysql_num_rows($stmt);
相当于
$count=mysql_num_rows($stmt);
in PDO? 在PDO中? I have included my code so you can see what I am doing but basically I am creating a login page for users I have a PDO statement using
SELECT
to search the database for a matching username and password row. 我已经包含了我的代码,因此您可以看到我在做什么,但是基本上我正在为用户创建一个登录页面,我有一个使用
SELECT
的PDO语句来在数据库中搜索匹配的用户名和密码行。 If it comes up with one then it will increase the $count
variable to one so that my IF
statment is true. 如果提供1,则它将
$count
变量增加为1,这样我的IF
语句为true。
CHECK_LOGIN.PHP 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 in FUNCTION.PHP 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));
}
Check example #2 from here: PDOStatement::rowCount 从此处检查示例2: PDOStatement :: rowCount
For most databases, PDOStatement::rowCount() does not return the number of rows affected by a SELECT statement.
对于大多数数据库,PDOStatement :: rowCount()不会返回受SELECT语句影响的行数。 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.
而是使用PDO :: query()发出与所要SELECT语句相同的谓词的SELECT COUNT(*)语句,然后使用PDOStatement :: fetchColumn()检索将返回的行数。 Your application can then perform the correct action.
然后,您的应用程序可以执行正确的操作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.