You could use
mysql_num_rows() to count the number of rows returned from a query.
I.e. If the number of rows returned is more than zero, a user exists.
The following example assumes that there is a table "users" with a column "username".
- PHP: Select all
<?php
$username = $_POST['username'];
$username = htmlspecialchars($username);
$username = mysql_real_escape_string($username);
/* database login details */
$db_username="username";
$db_password="password";
$database="mydatabase_db";
/* connect to database */
mysql_connect("localhost",$db_username,$db_password);
@mysql_select_db($database) or die( "Unable to select database");
$query = "SELECT * FROM users WHERE username = '$username'";
// pull all users from "users" table where the username equals the username supplied
$result = mysql_query($query);
$numrows = mysql_num_rows($result);
// number of rows returned
if ($numrows > 0) {
// if there are more than zero rows, the user exists
echo("Username exists");
}
if ($numrows == 0){
// if there are zero rows, the user doesn't exist
echo("Username does not exist");
}
else {
// if the number of rows is not zero or more, either it is an invalid format, or negative, therefore something must have gone wrong. :-)
echo("Mmmmm. Something went wrong.");
}
?>