Jump to content

Archived

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

MoFish

Check If Something Already Exists

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--]

Share this post


Link to post
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]

Share this post


Link to post
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]

Share this post


Link to post
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

Share this post


Link to post
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 :)

Share this post


Link to post
Share on other sites

×

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.