MoFish Posted April 20, 2006 Share Posted April 20, 2006 [!--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 https://forums.phpfreaks.com/topic/7934-check-if-something-already-exists/ Share on other sites More sharing options...
wisewood Posted April 20, 2006 Share Posted April 20, 2006 [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 thingif($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 https://forums.phpfreaks.com/topic/7934-check-if-something-already-exists/#findComment-28905 Share on other sites More sharing options...
predator12341 Posted April 20, 2006 Share Posted April 20, 2006 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 https://forums.phpfreaks.com/topic/7934-check-if-something-already-exists/#findComment-28961 Share on other sites More sharing options...
wisewood Posted April 20, 2006 Share Posted April 20, 2006 thats what i said... Link to comment https://forums.phpfreaks.com/topic/7934-check-if-something-already-exists/#findComment-28973 Share on other sites More sharing options...
kenrbnsn Posted April 20, 2006 Share Posted April 20, 2006 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 https://forums.phpfreaks.com/topic/7934-check-if-something-already-exists/#findComment-28974 Share on other sites More sharing options...
predator12341 Posted April 20, 2006 Share Posted April 20, 2006 [!--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 fullyand yep well said ken :) Link to comment https://forums.phpfreaks.com/topic/7934-check-if-something-already-exists/#findComment-28975 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.