Jump to content


Photo

How do you do a check on the database


  • Please log in to reply
11 replies to this topic

#1 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 16 April 2006 - 04:34 PM

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.
Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#2 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 16 April 2006 - 04:41 PM

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 FROM user_table WHERE users_row_name = "someNameHere" [!--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:
<?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";
}

?>
Also I would recommend you to change the row that stores your website users to be Unique.

#3 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 16 April 2006 - 04:44 PM

[!--quoteo(post=365329:date=Apr 16 2006, 04:41 PM:name=wildteen88)--][div class=\'quotetop\']QUOTE(wildteen88 @ Apr 16 2006, 04:41 PM) View Post[/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 FROM user_table WHERE users_row_name = "someNameHere" [!--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:
<?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";
}

?>
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.

Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#4 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 16 April 2006 - 05:07 PM

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 or die(mysql_error()) clause after you do mysql_query function MySQL will report back with an error.

#5 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 16 April 2006 - 05:24 PM

[!--quoteo(post=365335:date=Apr 16 2006, 05:07 PM:name=wildteen88)--][div class=\'quotetop\']QUOTE(wildteen88 @ Apr 16 2006, 05:07 PM) View Post[/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 or die(mysql_error()) 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.
Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#6 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 16 April 2006 - 05:35 PM

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.

#7 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 16 April 2006 - 06:25 PM

I have done the above i wanted to no
how to count the users name in the database thank you cheers.

my example to count number of users names cheers.
$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;
}

Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#8 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 16 April 2006 - 07:02 PM

So you want to see how many users are in your database? Then you can do this:
$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!";


#9 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 16 April 2006 - 07:17 PM

[!--quoteo(post=365360:date=Apr 16 2006, 07:02 PM:name=wildteen88)--][div class=\'quotetop\']QUOTE(wildteen88 @ Apr 16 2006, 07:02 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
So you want to see how many users are in your database? Then you can do this:
$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!";
[/quote]

how do i count the number of users via name cheers grate!

for example name is john
Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#10 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 16 April 2006 - 08:05 PM


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.
$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;
}
cheers!
Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#11 ypirc

ypirc
  • Members
  • PipPipPip
  • Advanced Member
  • 54 posts

Posted 16 April 2006 - 08:32 PM

I just editted your code a little, you don't need the second SQL statement.

$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;
}


#12 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 16 April 2006 - 09:03 PM


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.



$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;
}

Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users