Jump to content


Photo

Check If Something Already Exists


  • Please log in to reply
5 replies to this topic

#1 MoFish

MoFish
  • Members
  • PipPipPip
  • Advanced Member
  • 210 posts

Posted 20 April 2006 - 10:16 AM

[!--sizeo:5--][span style=\"font-size:18pt;line-height:100%\"][!--/sizeo--]PROBLEM SOLVED[!--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--]PROBLEM SOLVED[!--sizec--][/span][!--/sizec--]

#2 wisewood

wisewood
  • Members
  • PipPipPip
  • Advanced Member
  • 226 posts
  • LocationRotherham, England

Posted 20 April 2006 - 10:23 AM

<?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"; }

?>

wisewood: proven fact, I am both wise, and wooden.

#3 predator12341

predator12341
  • Members
  • PipPipPip
  • Advanced Member
  • 87 posts
  • LocationCheshunt, Hertfordshire, England

Posted 20 April 2006 - 01:45 PM


or you could do
<?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
}
?>

[a href="http://www.sloganizer.net/en/" target="_blank"][/a]

#4 wisewood

wisewood
  • Members
  • PipPipPip
  • Advanced Member
  • 226 posts
  • LocationRotherham, England

Posted 20 April 2006 - 02:09 PM

thats what i said...
wisewood: proven fact, I am both wise, and wooden.

#5 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 20 April 2006 - 02:17 PM

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, predator12341 checks the number of rows returned by the mysql_query() call.

Ken

#6 predator12341

predator12341
  • Members
  • PipPipPip
  • Advanced Member
  • 87 posts
  • LocationCheshunt, Hertfordshire, England

Posted 20 April 2006 - 02:17 PM

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

and yep well said ken :)
[a href="http://www.sloganizer.net/en/" target="_blank"][/a]




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users