View Full Version : Check if user exists

04-04-2005, 09:22 AM
Hey guys I want to write a php code that will check if a user exists in the database. Can someone tell me if there is an easier way to write this code?

$result = mysql_query("SELECT * FROM mpx_messages WHERE username='username'") or die(mysql_error());
$row = mysql_fetch_array( $result );
$mp_username = $row['username'];

if($mp_username == ""){
//user doesnt exist
//user exists

Im not sure but I think there should be an easier way to see if a user exists in a database.

04-04-2005, 09:26 AM
Here is what I use for one of my sites

However you get the username to the query is up to you.

$check = mysql_num_rows(mysql_query("SELECT username FROM members WHERE username='$user'"));

{ ?>
<font size="2" face="Arial, Helvetica, sans-serif">Sorry, the username <i><b><?php echo "$user"; ?></b></i>
is already taken.<br>
Please choose another one.</font><br>
<?php }
{ ?>
<font size="2" face="Arial, Helvetica, sans-serif">The username <i><b><?php echo "$user"; ?></b></i>
is available!<br>
You are good to go.</font><br>
<?php }

04-04-2005, 09:36 AM
AHA! I knew it, thanks :D