我正在设计一个相当简单的登录系统,我目前在用户尝试登录时使用以下代码来确定数据库中是否存在与用户尝试登录的用户名匹配的条目。(稍后在代码中,我检查匹配的密码等;我不担心那部分。)
目前,我使用 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);
谢谢你的帮助。如果我需要重新考虑这部分,请告诉我。我希望这对其他人有帮助。