我正在尝试设置一个用户登录页面(基本培训,因为我学习php),但页面无法正常工作,因为我认为我的部件有问题我计算行数 (我检查我的user_table数据库中输入的登录名和密码与已输入的登录名和密码匹配的行数= 1)。
我在PDO中,我不想使用being deprecated mysql_num_rows
,或者至少建议不要使用它。相反,我想使用Found_rows
这是我的login_page.php:
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="fr" lang="fr">
<head>
<title>LOGIN PAGE</title>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
</head>
<body>
<h1>Connect to your account</h1>
<form method="post" action="login_verify.php">
<input type="text" name="inputed_username" size="35" maxlength="30" required="required" value="Enter your login" /><br/><br/>
<input type="password" name="inputed_password" size="35" maxlength="30" required="required" value="Enter your password" /><br/><br/>
<input type="submit" name="submit "value="Connect me !" />
</form>
</body>
</html>
然后这是php文件,我在这个login_verify.php文件中检查输入的{login,password}
是否正确:
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="fr" lang="fr">
<head>
<title>login process check-up</title>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
</head>
<body>
<?php
if (isset($_POST['submit']))//if the button submit has not been clicked on
{
try
{
//database connection
$dbhost = "localhost";
$dbname = "test";
$dbuser = "root";
$dbpass = "";
$pdo_options[PDO::ATTR_ERRMODE] = PDO::ERRMODE_EXCEPTION;
$bdd = new PDO("mysql:host=$dbhost;dbname=$dbname; charset=UTF8",$dbuser,$dbpass,$pdo_options);
$bdd -> query('SET NAMES utf8');
//the database is checked to see if there is a match for the couple {user,password}
$user = $_POST['inputed_username'];
$password = $_POST['inputed_password'];
//preparaiton and execution of the request
$req = $bdd->prepare('SELECT * from user_table WHERE login= :login && password= :password LIMIT 1');
$req->execute(array(
'login' => $user,
'password' => $password));
$foundrows = $req->query("SELECT FOUND_ROWS()")->fetchColumn();//the number of rows are counted
//we check if there is a match for the login-password
if ($foundrows == 1)//if we find one that matches
{
session_start();
$_SESSION['username'] = $req['login'];
$_SESSION['fname'] = $req['first_name'];
$_SESSION['lname'] = $req['last_name'];
$_SESSION['logged'] = TRUE;
header("Location: first_page.php");
exit();
}
else //if we don't find any match
{
header("Location: login_page.php");
exit;
}
}
catch (Exception $e)
{
die('Erreur: '.$e->getMessage());
}
}
else //if the submit button has not been clicked on
{
header ("Location: login_page.php");
exit;
}
?>
如果你们中任何人知道问题所在,那就太棒了。
我认为它来自于滥用found_rows。这是我第一次使用它,所以我觉得我有些不好,但我不知道它是什么。
答案 0 :(得分:1)
FOUND_ROWS()
仅在您在select查询中具有SQL_CALC_FOUND_ROWS
选项的select查询之后使用它时才有效,并且只有在它是LIMIT查询时才真正有用。 FOUND_ROWS()是如果不是limit子句则返回WOULD的行数。
SELECT SQL_CALC_FOUND_ROWS ... WHERE ... LIMIT 10
SELECT FOUND_ROWS()
对于非LIMITed查询,请改用mysql_num_rows()
。
据推测,您的数据库设置为login
是主要/唯一关键字段,因此您的查询最多只返回一行,因此限制毫无意义。