Page 1 of 1

How to check if $var exist in a field

Posted: Fri Dec 26, 2003 4:20 am
by vigge89
I'm creating a register-system right now for mysite, but i have no clue on how to check if a username already exist in the field 'nick'. How could I do it?

Posted: Fri Dec 26, 2003 7:19 am
by aquila125
You could use the UNIQUE keyword.. or just do a select query on the usertable with the username in the where clause

Posted: Fri Dec 26, 2003 10:14 am
by vigge89
how do you use the unique keyword? i've assigned it to the nickname, but how should i check it?

Posted: Fri Dec 26, 2003 10:31 am
by Nay
Have you tried using the SELECT as you normally would. What's the different by not using it anyhow. Here's something:

Code: Select all

<?php

$nick = $_GET['nick'];

$query = "SELECT nick FROM users WHERE nick = '$nick'";
$result = mysql_query($query, $link);
$number = mysql_num_rows($result);

if($number == 0) {
print "there's nothing there";
} elseif($number == 1) {
print "someone's already taken that";
} else {
print "uh-oh, somethings wrong, $number people with the same nick!";
}

?>
-Nay

Posted: Fri Dec 26, 2003 5:09 pm
by vigge89
okay, ill test it out soon :D