Jump to content

Checking for Existing Row


waiwai933

Recommended Posts

Hi everyone! :D I need a little help with a new script of mine. How do I get it so that the script, once connected to the db & table, checks if a row exists. For example, if I'm allowing users to register, I don't want them to have the same username. :P So, I need to check if that username already exists, by connecting to the database and selecting the table. How do I get a variable that tells me if that username has been taken up, if let's say, each username is stored in a field called 'user'.

Link to comment
https://forums.phpfreaks.com/topic/136145-checking-for-existing-row/
Share on other sites

That's not PHP (for the most part); that's MySQL.

 

 

What exactly do you not get about it?

 

You would use it like:

 

$username = $_POST['username'];
$unames = mysql_real_escape_slashes($username);
$q = mysql_query("SELECT COUNT(username) as cnt FROM users WHERE user = '{$unames}'");
if(mysql_result($q, 0, 0) == 1)) {
    //name exists.
}

 

 

Another, possibly better way, would be to do SELECT 1 FROM users WHERE user = ''; and then check the row count.

Actually Corbin, your rewriting it made it make sense. :D Thanks. Actually, I'm new to both PHP and MySQL. I have just one question. What does the second line, the one that says

$unames = mysql_real_escape_slashes($username);

 

What does that line do? I can't find that command on Google.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.