0

我正在尝试设置一个用户登录页面(我学习 php 时的基本培训),但该页面无法正常工作,因为我认为我在计算行数的部分存在问题(我检查了我的行数输入的登录名和密码与已输入的匹配的用户表数据库=1)。

我在 PDO 中,我不想使用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,password}此 login_verify.php 文件中输入的内容是否正确:

<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的滥用。这是我第一次使用它,所以我觉得我的东西很糟糕,但我不知道它是什么。

4

1 回答 1

1

FOUND_ROWS()仅在您在选择查询中有SQL_CALC_FOUND_ROWS选项的选择查询之后使用它时才有效,并且只有在它是 LIMIT 查询时才真正有用。FOUND_ROWS() 是如果没有限制子句,将返回的行数。

SELECT SQL_CALC_FOUND_ROWS ... WHERE ... LIMIT 10
SELECT FOUND_ROWS()

对于非 LIMITed 查询,请mysql_num_rows()改用。

大概您的数据库设置login为主/唯一键字段,因此您的查询无论如何只会返回最多一行,因此限制是没有意义的。

于 2012-07-14T22:11:05.290 回答