Jump to content

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.