redarrow Posted April 16, 2006 Share Posted April 16, 2006 How do you do a check on the database to see if a name exist then echo a message cheers.Your ansaw will be studed for learning perpose cheers. Quote Link to comment https://forums.phpfreaks.com/topic/7549-how-do-you-do-a-check-on-the-database/ Share on other sites More sharing options...
wildteen88 Posted April 16, 2006 Share Posted April 16, 2006 You can do a SELECT query but use the WHERE clause ie:[!--sql--][div class=\'sqltop\']SQL[/div][div class=\'sqlmain\'][!--sql1--][span style=\'color:blue;font-weight:bold\']SELECT[/span] users_row_name [color=green]FROM[/color] [color=orange]user_table[/color] [color=green]WHERE[/color] users_row_name [color=orange]=[/color] [color=red]"someNameHere"[/color] [!--sql2--][/div][!--sql3--]Then in PHP you use the mysql_num_rows function and check whether the query returned more than one name. Heres some code:[code]<?php$query = "SELECT users_row_name FROM user_table WHERE users_rwo_name = "someNameHere";$result = mysql_query($query);if(mysql_num_rows($result) != '1'){ echo "unfortunatly that name has alread been taken, please try another";}else{ echo "Hay! You're in luck that name is available";}?>[/code]Also I would recommend you to change the row that stores your website users to be Unique. Quote Link to comment https://forums.phpfreaks.com/topic/7549-how-do-you-do-a-check-on-the-database/#findComment-27494 Share on other sites More sharing options...
redarrow Posted April 16, 2006 Author Share Posted April 16, 2006 [!--quoteo(post=365329:date=Apr 16 2006, 04:41 PM:name=wildteen88)--][div class=\'quotetop\']QUOTE(wildteen88 @ Apr 16 2006, 04:41 PM) [snapback]365329[/snapback][/div][div class=\'quotemain\'][!--quotec--]You can do a SELECT query but use the WHERE clause ie:[!--sql--][div class=\'sqltop\']SQL[/div][div class=\'sqlmain\'][!--sql1--][span style=\'color:blue;font-weight:bold\']SELECT[/span] users_row_name [color=green]FROM[/color] [color=orange]user_table[/color] [color=green]WHERE[/color] users_row_name [color=orange]=[/color] [color=red]"someNameHere"[/color] [!--sql2--][/div][!--sql3--]Then in PHP you use the mysql_num_rows function and check whether the query returned more than one name. Heres some code:[code]<?php$query = "SELECT users_row_name FROM user_table WHERE users_rwo_name = "someNameHere";$result = mysql_query($query);if(mysql_num_rows($result) != '1'){ echo "unfortunatly that name has alread been taken, please try another";}else{ echo "Hay! You're in luck that name is available";}?>[/code]Also I would recommend you to change the row that stores your website users to be Unique.[/quote]Thank you is there a shorter way cheers. Quote Link to comment https://forums.phpfreaks.com/topic/7549-how-do-you-do-a-check-on-the-database/#findComment-27495 Share on other sites More sharing options...
wildteen88 Posted April 16, 2006 Share Posted April 16, 2006 Yes there, and that is to change your users_row_name row to a unique identifier. You can do this in PHPMyAdmin by editing the users table and clicking the Unique Identidier option on theu sers_row_name row.This way MySQL will check when it inserts a new record that the username being inserted doesn't already exist users_row_name row in the database. If there is then if you have the [i]or die(mysql_error())[/i] clause after you do mysql_query function MySQL will report back with an error. Quote Link to comment https://forums.phpfreaks.com/topic/7549-how-do-you-do-a-check-on-the-database/#findComment-27500 Share on other sites More sharing options...
redarrow Posted April 16, 2006 Author Share Posted April 16, 2006 [!--quoteo(post=365335:date=Apr 16 2006, 05:07 PM:name=wildteen88)--][div class=\'quotetop\']QUOTE(wildteen88 @ Apr 16 2006, 05:07 PM) [snapback]365335[/snapback][/div][div class=\'quotemain\'][!--quotec--]Yes there, and that is to change your users_row_name row to a unique identifier. You can do this in PHPMyAdmin by editing the users table and clicking the Unique Identidier option on theu sers_row_name row.This way MySQL will check when it inserts a new record that the username being inserted doesn't already exist users_row_name row in the database. If there is then if you have the [i]or die(mysql_error())[/i] clause after you do mysql_query function MySQL will report back with an error.[/quote]Thank you can you also kindly show how to count the number of times the user name is in the database thank you. Quote Link to comment https://forums.phpfreaks.com/topic/7549-how-do-you-do-a-check-on-the-database/#findComment-27503 Share on other sites More sharing options...
wildteen88 Posted April 16, 2006 Share Posted April 16, 2006 You use my previous SQL Query with mysql_num_rows. You dont need to count how many times of the same username is in the database, becuase of the unique identifier option will only 1 occurance of the same username in the database. Quote Link to comment https://forums.phpfreaks.com/topic/7549-how-do-you-do-a-check-on-the-database/#findComment-27504 Share on other sites More sharing options...
redarrow Posted April 16, 2006 Author Share Posted April 16, 2006 I have done the above i wanted to nohow to count the users name in the database thank you cheers.my example to count number of users names cheers.[code]$db=mysql_connect("xxxx","xxxxx","xxxx");mysql_select_db("schollwork",$db);$query="SELECT name from work";$result=mysql_query($query);for($i=0; $i<=count($result); $i++){echo "<br>'".$name[$i]."'<br>";exit;}[/code] Quote Link to comment https://forums.phpfreaks.com/topic/7549-how-do-you-do-a-check-on-the-database/#findComment-27514 Share on other sites More sharing options...
wildteen88 Posted April 16, 2006 Share Posted April 16, 2006 So you want to see how many users are in your database? Then you can do this:[code]$db = mysql_connect("xxxx","xxxxx","xxxx");mysql_select_db("schollwork",$db);$query = "SELECT COUNT(name) as users FROM work";$result = mysql_query($query);$user = mysql_fetch_array($result);echo "There are <b>" . $user['users'] . "</b> users currently in the database!";[/code] Quote Link to comment https://forums.phpfreaks.com/topic/7549-how-do-you-do-a-check-on-the-database/#findComment-27524 Share on other sites More sharing options...
redarrow Posted April 16, 2006 Author Share Posted April 16, 2006 [!--quoteo(post=365360:date=Apr 16 2006, 07:02 PM:name=wildteen88)--][div class=\'quotetop\']QUOTE(wildteen88 @ Apr 16 2006, 07:02 PM) [snapback]365360[/snapback][/div][div class=\'quotemain\'][!--quotec--]So you want to see how many users are in your database? Then you can do this:[code]$db = mysql_connect("xxxx","xxxxx","xxxx");mysql_select_db("schollwork",$db);$query = "SELECT COUNT(name) as users FROM work";$result = mysql_query($query);$user = mysql_fetch_array($result);echo "There are <b>" . $user['users'] . "</b> users currently in the database!";[/code][/quote]how do i count the number of users via name cheers grate!for example name is john Quote Link to comment https://forums.phpfreaks.com/topic/7549-how-do-you-do-a-check-on-the-database/#findComment-27533 Share on other sites More sharing options...
redarrow Posted April 16, 2006 Author Share Posted April 16, 2006 How can i combine these two statements together, i wont to show the user that there is a user in the database via a number and not let that entry go into the database.[code]$db=mysql_connect("localhost","xxxx","xxxx");mysql_select_db("schollwork",$db);$query = "SELECT COUNT(name) as users FROM work where name='$name'";$result = mysql_query($query);$user = mysql_fetch_array($result);echo "<br>There are <b>" . $user['users'] . "</b> same username's in the database";mysql_select_db("schollwork",$db);$query = "SELECT name FROM work where name='$name'";$result = mysql_query($query);if(mysql_num_rows($result)!==1){echo" sorry $name name tacken";echo"<br><my url back to the form>Please try agin</a>";exit;}[/code]cheers! Quote Link to comment https://forums.phpfreaks.com/topic/7549-how-do-you-do-a-check-on-the-database/#findComment-27545 Share on other sites More sharing options...
ypirc Posted April 16, 2006 Share Posted April 16, 2006 I just editted your code a little, you don't need the second SQL statement.[code]$query = "SELECT COUNT(name) as users FROM work where name='$name'";$result = mysql_query($query);$user = mysql_fetch_array($result);echo "<br>There are <b>" . $user['users'] . "</b> same username's in the database";if($user['users'] >= 1){ echo" sorry $name name taken"; echo"<br><my url back to the form>Please try agin</a>"; exit;}[/code] Quote Link to comment https://forums.phpfreaks.com/topic/7549-how-do-you-do-a-check-on-the-database/#findComment-27554 Share on other sites More sharing options...
redarrow Posted April 16, 2006 Author Share Posted April 16, 2006 This is it complete but wont to shorten it down please help cheers.If a user enters a user name tacken they get a message, Also if the user enter a email address tacken the user gets a message, If the user gets smart and enter a diffrent username with an existing email address the user gets a message And finally if the user enter reall email but with an existing username the user gets a message.solved but nedds to be shortend like above cheers.[code]$db=mysql_connect("localhost","xxxx","xxxx");mysql_select_db("schollwork",$db);$query = "SELECT email FROM work where email='$email'";$result = mysql_query($query);if(mysql_num_rows($result)> 1){echo"<b> sorry $email email tacken";echo"<br><a href='my url'>Please try agin</a>";exit;}$db=mysql_connect("localhost","xxxx","xxxx");mysql_select_db("schollwork",$db);$query = "SELECT COUNT(name) as users FROM work where name='$name'";$result = mysql_query($query);$user = mysql_fetch_array($result);echo "<b>There are <b>" . $user['users'] . "</b> ";$db=mysql_connect("localhost","xxxx","xxxx");mysql_select_db("schollwork",$db);$query = "SELECT name FROM work where name='$name'";$result = mysql_query($query);if(mysql_num_rows($result)==1){echo" sorry $name name tacken";echo"<br><a href='my url";die;}[/code] Quote Link to comment https://forums.phpfreaks.com/topic/7549-how-do-you-do-a-check-on-the-database/#findComment-27561 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.