PHP/MySQL - 登录系统 - SELECT 整个数据库是否聪明?

标签 php mysql

我正在设计一个相当简单的登录系统,目前我在用户尝试登录时使用以下代码来确定数据库中是否存在与用户尝试登录的用户名相匹配的条目在与。 (在代码的后面,我检查匹配的密码等;我不担心那部分。)

目前,我使用 SELECT 将整个数据库抓取到一个变量($wholeUserDatabase)中,然后遍历它以确定“用户名”字段是否匹配。

它现在工作很好。但是我的数据库现在有三个用户。当我向公众发布站点并(理论上)获得更多用户时,这种将整个数据库抓取到变量中的方法会变得非常慢吗?

$connection = mysql_connect($mysql_host,$mysql_user,$mysql_password);

mysql_select_db($mysql_database, $connection);

// Take the whole user database, and store it in $wholeUserDatabase.
$wholeUserDatabase = mysql_query("SELECT * FROM myTable")
    or die(mysql_error());  

$boolFoundUser = false;

/* Iterate once for every entry in the database, storing the current entry 
of the database into a variable $currentEntry, which is an array containing 
everything related to the one user. */
while($currentEntry = mysql_fetch_array($wholeUserDatabase)) {

    /* Does the "username" field of the current entry match the one 
    the user tried to log in with? */
    if ($currentEntry['username'] == $_POST['username']) {

        /* If it does, break the loop so that the $currentEntry variable 
        will contain the information for the user who is trying to log in,
        which I will later need to check passwords, etc. */
        $boolFoundUser = true;
        break;
    }
}

mysql_close($connection);

感谢您的帮助。如果我需要重新考虑这部分,请告诉我。我希望这对其他人有帮助。

最佳答案

是的!这将非常非常缓慢。不要选择整个数据库,只选择你需要的。

关于PHP/MySQL - 登录系统 - SELECT 整个数据库是否聪明?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11404450/

相关文章:

mysql : innodb corrupted data - recovery failing

mysql - 如何使用 mysql 获取最近 5 天的总计?

javascript - 饼图在 php 页面中重叠

php - 我如何通过 PHP 使用来自 Facebook 的 JSON 对象?

javascript - 以pdf格式发送html代码的代码是什么

php - 使用 PHP 在第 7 天使 Cookie 过期

java - 论坛设计的混合内容警告

php - HTML 表单提交的问题

mysql - 结果集包含一行的每个不同实例的一列。对于mysql中的每个like结构

mysql - 如何使用mysql select查询获取下一行中的后续列总数