How to check if $var exist in a field

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

How to check if $var exist in a field

Post 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?
User avatar
aquila125
Forum Commoner
Posts: 96
Joined: Tue Dec 09, 2003 10:39 am
Location: Belgium

Post by aquila125 »

You could use the UNIQUE keyword.. or just do a select query on the usertable with the username in the where clause
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Post by vigge89 »

how do you use the unique keyword? i've assigned it to the nickname, but how should i check it?
Nay
Forum Regular
Posts: 951
Joined: Fri Jun 20, 2003 11:03 am
Location: Brisbane, Australia

Post 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
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Post by vigge89 »

okay, ill test it out soon :D
Post Reply