ted_chou12 Posted December 24, 2006 Share Posted December 24, 2006 I am very new to this, so I wish to ask a simple question about mysql.I have built a table already, however, when signup, I wish that the php script could search for any repetitions in username before it lets the person use that username.ThanksTed. Quote Link to comment https://forums.phpfreaks.com/topic/31767-mysql-search-for-repetition-in-username-column/ Share on other sites More sharing options...
HuggieBear Posted December 24, 2006 Share Posted December 24, 2006 When they submit the membership form, use a query like this:[code]<?php$sql = "SELECT unique_id_column FROM member_table WHERE username = '{$_POST['username']}'";$result = mysql_query($sql);?>[/code]Then use the mysql_num_rows() function...[code]<?phpif (mysql_num_rows($result) > 0){ echo "Username taken\n";else { // Insert the new user into the database}?>[/code]RegardsHuggie Quote Link to comment https://forums.phpfreaks.com/topic/31767-mysql-search-for-repetition-in-username-column/#findComment-147309 Share on other sites More sharing options...
ted_chou12 Posted December 24, 2006 Author Share Posted December 24, 2006 Thanks HuggieBear, but I have a small question:[code]mysql_num_rows($result) > 1[/code]what I dont understand here is the ">" symbols and the number "1", could anyone explain it to me?ThanksTed. Quote Link to comment https://forums.phpfreaks.com/topic/31767-mysql-search-for-repetition-in-username-column/#findComment-147312 Share on other sites More sharing options...
HuggieBear Posted December 24, 2006 Share Posted December 24, 2006 I edited that code slightly...mysql_num_rows() returns the number of rows found when the query's executed. If it found more than 0 rows (> 0) then the username must exist, meaning you can't have that username.RegardsHuggie Quote Link to comment https://forums.phpfreaks.com/topic/31767-mysql-search-for-repetition-in-username-column/#findComment-147313 Share on other sites More sharing options...
ted_chou12 Posted December 24, 2006 Author Share Posted December 24, 2006 thanks,I am able to replce:member_tableusernamebut I dont understand:unique_id_columnWhat should I replace this with, on a tutorial they just have a * instead of words... ???Thanks Quote Link to comment https://forums.phpfreaks.com/topic/31767-mysql-search-for-repetition-in-username-column/#findComment-147318 Share on other sites More sharing options...
kamasheto Posted December 24, 2006 Share Posted December 24, 2006 Basically it saves query usage if you insert a name (or you called it a word?)What unique_id_column is, is just a column name in your table, it's doesn't actually matter what column you're calling for as long as your WHERE clause is correctly taken care of. For example, if my table looks like this;[table][tr][td]id[/td][td]username[/td][td]password[/td][/tr][tr][td]1[/td][td]kamasheto[/td][td]password[/td][/tr][tr][td]2[/td][td]username[/td][td]password[/td][/tr][/table]And I try registering the username kamasheto with this query;[code]SELECT id FROM members WHERE username='kamasheto';[/code]Then I'll get this row returning;[table][tr][td]id[/td][/tr][tr][td]1[/td][/tr][/table]This when passed through mysql_num_rows(); will give me a true value of 1, which will produce an error as desired. If you decide you want to select * (means all the columns), your query;[code]SELECT * FROM members WHERE username='kamasheto';[/code]will return[table][tr][td]id[/td][td]username[/td][td]password[/td][/tr][tr][td]1[/td][td]kamasheto[/td][td]password[/td][/tr][/table]Which when passed through mysql_num_rows() will give the same result, 1 returned row. The only limitation to calling a specific column(s) is that in your fetching process (using mysql_fetch_array with the while loop), you'll be able to only use those columns. For example in my first query where I called for ONLY the id, I cannot check for the password using that same query, I'll have to call for a new query hence. However with my second query, I can always fetch the entire array anytime down the script.Helpful enough? Quote Link to comment https://forums.phpfreaks.com/topic/31767-mysql-search-for-repetition-in-username-column/#findComment-147321 Share on other sites More sharing options...
ted_chou12 Posted December 24, 2006 Author Share Posted December 24, 2006 oh, thanks ;) I understand it completely now. Quote Link to comment https://forums.phpfreaks.com/topic/31767-mysql-search-for-repetition-in-username-column/#findComment-147323 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.