Ken2k7 Posted August 23, 2007 Share Posted August 23, 2007 Say I have created a database table named member with columns - id, name, age, sex - and say someone registers and I want to see if that name already exists. I assume I can do this: <?php $name = "Ken2k7" $sql = "SELECT name FROM member WHERE name='$name'"; ?> I think that would work, but if the name doesn't exist what value would $sql have? What value would $sql have if the name does exist? Link to comment https://forums.phpfreaks.com/topic/66301-solved-available-name/ Share on other sites More sharing options...
XaeroDegreaz Posted August 23, 2007 Share Posted August 23, 2007 $query = mysql_query("SELECT * FROM `member` WHERE `name` = '$name' LIMIT 1"); if(mysql_num_rows($query) == 0) { //No name exists, they can register! }else { echo("Username already exists, please pick another."); } If you are trying to retrieve the values of your query: $query = mysql_query("SELECT * FROM `member` WHERE `name` = '$name' LIMIT 1"); $result = mysql_fetch_array($query); echo($result["name"]); // would echo the name that is in the database echo($result["age"]); //age If there are no results, then php will simply display nothing. Link to comment https://forums.phpfreaks.com/topic/66301-solved-available-name/#findComment-331670 Share on other sites More sharing options...
matthewhaworth Posted August 23, 2007 Share Posted August 23, 2007 XaeroDegreaz, I don't think that was what he was after.. he wanted to know.. will it implement $name is the variable $name doesn't exist? Try it.. it doesn't have to be a $sql $name = "matthew"; $sentence = "my name is $name"; echo $sentence; // Outputs : my name is matthew //$name = "matthew"; $sentence = "my name is $name"; echo $sentence; // Outputs : ?? I always regard it bad programming to do that though, and prefer to explicitly concatenate my strings: $name = "matthew"; $age = 16; $sentence = "My name is ".$name." and I am ".$age." years old."; echo $sentence; Link to comment https://forums.phpfreaks.com/topic/66301-solved-available-name/#findComment-331671 Share on other sites More sharing options...
Ken2k7 Posted August 23, 2007 Author Share Posted August 23, 2007 Maybe I should've worded my question better. However, XaeroDegreaz answered my question. Thank you. And matthewhaworth, sorry about the confusion. I thank you for helping nonetheless. Link to comment https://forums.phpfreaks.com/topic/66301-solved-available-name/#findComment-331676 Share on other sites More sharing options...
matthewhaworth Posted August 23, 2007 Share Posted August 23, 2007 Maybe I should've worded my question better. However, XaeroDegreaz answered my question. Thank you. And matthewhaworth, sorry about the confusion. I thank you for helping nonetheless. Pfft, you suck lol. Link to comment https://forums.phpfreaks.com/topic/66301-solved-available-name/#findComment-331677 Share on other sites More sharing options...
XaeroDegreaz Posted August 23, 2007 Share Posted August 23, 2007 Woot, score one for the new guy. What goes around should come around. Someone answer my question now =}~ Link to comment https://forums.phpfreaks.com/topic/66301-solved-available-name/#findComment-331686 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.