Jump to content

Check If Something Already Exists


MoFish

Recommended Posts

[!--sizeo:5--][span style=\"font-size:18pt;line-height:100%\"][!--/sizeo--][b]PROBLEM SOLVED[/b][!--sizec--][/span][!--/sizec--]

hey. heres the scenario. I would like to check if a username already exists on the registration section of my website and would also like to check if a thread is created with the same name on another section. im unsure exactly how I go around looping through the rows checking for a matching value and flagging the error up if found. does anyone mind showing me how this is done with some dummy variables and i'll give it a go for myself?

thanks again, mofish.

[!--sizeo:5--][span style=\"font-size:18pt;line-height:100%\"][!--/sizeo--][b]PROBLEM SOLVED[/b][!--sizec--][/span][!--/sizec--]
Link to comment
Share on other sites

[code]
<?php

// something like this...
// when form is submitted

// query the db for matching data
$check = "SELECT * FROM your_table WHERE title = $_POST[title]";
$result = mysql_query($check);

// if it exists, do one thing
if($result==1){ echo "An entry with this name already exists, please try something else";}

// if it doesnt exist, do something else.
else { echo "No entry exists, so we can continue"; }

?>
[/code]
Link to comment
Share on other sites


or you could do
[code]
<?php

// something like this...
// when form is submitted

// query the db for matching data
$check = "SELECT * FROM your_table WHERE title = $_POST[title]";
$result = mysql_query($check);
$num = mysql_num_rows($result);

if($num >=1)
{
    do your first thing
}
  else
{
    do something else
}
?>
[/code]
Link to comment
Share on other sites

No, what you said was to check the $result from the mysql_query() call which will probably not be the correct thing to do. When there are no rows to be returned by the query, the call returns a pointer to an empty set.

On the other hand, [b]predator12341[/b] checks the number of rows returned by the mysql_query() call.

Ken
Link to comment
Share on other sites

[!--quoteo(post=366826:date=Apr 20 2006, 03:09 PM:name=wisewood)--][div class=\'quotetop\']QUOTE(wisewood @ Apr 20 2006, 03:09 PM) [snapback]366826[/snapback][/div][div class=\'quotemain\'][!--quotec--]
thats what i said...
[/quote]
sort of but not fully

and yep well said ken :)
Link to comment
Share on other sites

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.