Hi I need a little help with my sql code and php. I made script that adds a username to my mysql database and it checks for illegal characters which works. Now I want to also check the database to see if this username is already in the database. Heres what I have:
<?php
$temp = "SELECT * FROM userdata WHERE MATCH (user) AGAINST ('$user'); ";
if (mysql_query($temp,$lk) > 0){
//Code that redirects to previous page with an argument telling
//me this user name already exists
}
else
echo mysql_error();
?>
Where userdata is my table name, user is the column, and $user is the username that Im passing from the previous page.
I think that my mysql syntax is correct but I dont know what to set in my if statement to know if the user is in there or not. Ive tried echoing the query but all I get is Resource id#3. If Im going at this the wrong way please tell me. I just want to search a database for a username and Ive been working on this for 3 days now, and nothing works. Any help would be appreciated.
<?php
$query = mysql_query("SELECT * FROM `userdata` WHERE `user` = '$user' LIMIT 1") or die(mysql_error());
if(mysql_num_rows($query))
die("$user already taken");
// perform insert
?>
that code will only check for exactly duplicate names. So if "Foo" is a user, and "foo" is this new user, it'll be allowed to insert. If you wish to disallow case, use the LIKE keyword instead of =.. You'll have to sanitize/escape mysql wildcard characters in $user.
alternately, if the `user` field is an index, I believe trying to insert will fail if already taken..
Last edited by feyd on Sat Jun 19, 2004 8:32 pm, edited 1 time in total.
<?php
$sql = "SELECT * FROM users_table WHERE username='$name'";
$result = mysql_query($sql);
$row = mysql_fetch_array($result);
$user = $row['username'];
if (!$user == "") {
echo "The username you have choosen has already been taken.";
}
?>
lol well u would have to add in your exact information so it can query to the correct table, but mysql_num_rows is perfect with exception to the cases (upper n lower)