What are the advantages of using Count(*) with a limit of 1 over mysql_num_rows() in PHP login scripts?
When checking if a user exists in a database during a login process, using `COUNT(*)` with a limit of 1 is more efficient than using `mysql_num_rows()`. This is because `COUNT(*)` simply returns the number of rows that match the query, while `mysql_num_rows()` fetches and stores all the rows in memory before counting them. Therefore, using `COUNT(*)` with a limit of 1 reduces memory usage and improves performance in PHP login scripts.
// Using COUNT(*) with a limit of 1 to check if a user exists in the database
$query = "SELECT COUNT(*) FROM users WHERE username = '$username' LIMIT 1";
$result = mysqli_query($connection, $query);
$row = mysqli_fetch_array($result);
if ($row[0] > 0) {
// User exists
} else {
// User does not exist
}