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