navigation
a webmaster learning community
     Home    Register     Search      Help      Login    
FrontPage Alternative
Sponsors

Shopping Cart Software
Ecommerce software integrated into Frontpage, Dreamweaver and Golive templates. No monthly fees and available in ASP and PHP versions.

Website Templates
We also have a wide selection of Dreamweaver, Expression Web and Frontpage templates as well as webmaster tools and CSS layouts.

Frontpage website templates
Creative Website Templates for FrontPage, Dreamweaver, Flash, SwishMax

Search Forums
 

Advanced search
Recent Posts

 Todays Posts
 Most Active posts
 Posts since last visit
 My Recent Posts
 Mark posts read

 

*SOLVED* Check Against Records (MySQL)

 
View related threads: (in this forum | in all forums)

Logged in as: Guest
Users viewing this topic: none
Printable Version 

All Forums >> Web Development >> ASP and Database >> *SOLVED* Check Against Records (MySQL)
Page: [1]
 
Ryokotsusai

 

Posts: 248
Joined: 10/5/2005
Status: offline

 
*SOLVED* Check Against Records (MySQL) - 4/23/2006 14:09:34   
Hi,

is there a simple way of comparing a user input to entries in a database and stopping them if they match?

such as [forgive me if im way off with this]
if ( $uname == *info from db* ) {
die ( "Username is already in use" );
} else {
echo "Username checks out";
}


how would you compare it to all of the like entries in the db?

< Message edited by Ryokotsusai -- 4/24/2006 13:21:14 >


_____________________________

The world is more like it is now than it ever has been before.
--Dwight Eisenhower
yb2

 

Posts: 653
Joined: 1/30/2006
Status: offline

 
RE: Check Against Records (MySQL) - 4/23/2006 15:15:49   
quote:

is there a simple way of comparing a user input to entries in a database and stopping them if they match?


put the conditional statement into a function, and when it gets to the bit you want it to stop at use the RETURN keyword, or use a WHILE loop, and have it do the loop while $uname == whatever is not true.

quote:

how would you compare it to all of the like entries in the db?


The first method you've implied you're using (above) would mean you'd taken all the data out of the database, probably put it into an array and would loop through and check it record by record.

If you wanted to check 1 particular value against all the database entries, then you would use the database engine to do it instead of PHP since databases are optimised for that, with a SQL statement and probably IF EXISTS

CREATE PROCEDURE CheckUserNameAlreadyExists_usp
( IN Username VARCHAR(120), OUT myretval BOOLEAN )
SET myretval = false;
IF EXISTS ( SELECT name FROM Table WHERE name = $uname )
BEGIN
SET myretval = true;
END

you'd need to be using mySQL 5 to use this, put it into a stored procedure and call it from your PHP. I don't use mySQL much so you will need to check the syntax out properly.

_____________________________

it is natural for people not to see one's own faults, and to exaggerate other people's faults and failings.
Currently listening to: L'Enfer Des Formes by Stereolab

(in reply to Ryokotsusai)
Ryokotsusai

 

Posts: 248
Joined: 10/5/2005
Status: offline

 
RE: Check Against Records (MySQL) - 4/23/2006 19:31:51   
thank you for your response, but...

:), I am new to this, and I have no idea what you are telling me to do

The loop idea i like, but i don't know how i would stop it if it didn't find the record...

and what is a stored procedure?

thank u



_____________________________

The world is more like it is now than it ever has been before.
--Dwight Eisenhower

(in reply to yb2)
yb2

 

Posts: 653
Joined: 1/30/2006
Status: offline

 
RE: Check Against Records (MySQL) - 4/23/2006 20:32:27   
no probs. If you are trying to check 1 username against all the usernames in your database then forget the loop for now - let the database do the work!

A stored procedure is what is says!:) You can write a SQL statement(s) - the procedure - that you will use many times and store it on the database, like a function/procedure/method - whatever you wish to call it.

The reason you need to use a stored procedure is because you are using a conditional statement in the SQL:
If there is a username in the database, then tell me (output true), if not then tell me (output false).

With the stored procedure I wrote above, you put IN the username, the database engine checks it against the data, and it OUT comes whether it's true or false (it's already here=true, or it's not = false).

Have a read of this helpful tutorial and see what you think.

_____________________________

it is natural for people not to see one's own faults, and to exaggerate other people's faults and failings.
Currently listening to: L'Enfer Des Formes by Stereolab

(in reply to Ryokotsusai)
Ryokotsusai

 

Posts: 248
Joined: 10/5/2005
Status: offline

 
RE: Check Against Records (MySQL) - 4/23/2006 22:26:51   
How do you store a procedure in MySQL?

Edit>> and it looks like i only have mysql 4 something

< Message edited by Ryokotsusai -- 4/23/2006 22:35:45 >


_____________________________

The world is more like it is now than it ever has been before.
--Dwight Eisenhower

(in reply to yb2)
yb2

 

Posts: 653
Joined: 1/30/2006
Status: offline

 
RE: Check Against Records (MySQL) - 4/24/2006 6:35:10   
use the CREATE PROCEDURE command, but if you've only got mySQL 4 then you can't do it. Forget that one too! :)

Try this instead. Run the following SELECT statement against the database
<?
function CheckUserNameAlreadyExists( $uname ) {
//build the sql statement
//it gets the number of times the username is in the database
//if greater than 0 then it's in there
$sql = "SELECT Count(*) FROM YourTable WHERE Username = " . $uname;
//connect to the database
$conn = mysql_connect( "hostname, "username" "password" );
//pick the database to use
mysql_select_db( "yourdb", $conn );
//execute the sql statement
$result = mysql_query( $sql, $conn);
//check the result
if ( $result >= 1 )
{
$message = "That username is already in use!";
}
else
{
$message = "That username checks out";
}
//output the result
return $message;
}//end of function

//get a username
$uname = "test username"
//call the function
echo CheckUserNameAlreadyExists( $uname );
//will print out whether username is in use or not
?>

I don't use php a lot, so please check the syntax, but that should give you an idea of what to do.

_____________________________

it is natural for people not to see one's own faults, and to exaggerate other people's faults and failings.
Currently listening to: L'Enfer Des Formes by Stereolab

(in reply to Ryokotsusai)
Ryokotsusai

 

Posts: 248
Joined: 10/5/2005
Status: offline

 
RE: Check Against Records (MySQL) - 4/24/2006 11:00:07   
Ok,
this is what i have so far, but it always tells me that the username checks out, even when it shouldn't

<?
//Connection Settings

$sqname="************";
$sqpass="********";
$sqdata="************";

mysql_connect("12.34.56.78",$sqname,$sqpass);
@mysql_select_db($sqdata) or die("Unable to select database");

//Data From Html

$fname = $_POST['FN'];
$lname = $_POST['LN'];
$uname = $_POST['UN'];
$upass = $_POST['PASS'];
$cpass = $_POST['PASS2'];
$email = $_POST['EM'];

$url = $_POST['URL'];
$aim = $_POST['AIM'];
$msn = $_POST['MSN'];
$yah = $_POST['YAH'];
$not = $_POST['NOT'];

//Functions

function CheckUserNameAlreadyExists( $uname ) {
//build the sql statement
//it gets the number of times the username is in the database
//if greater than 0 then it's in there
$sql = sprintf("SELECT COUNT(*) AS test FROM Users WHERE Username = %s ", $uname);
//connect to the database
$conn = mysql_connect("65.98.98.58",$sqname,$sqpass);
@mysql_select_db($sqdata);
$result = mysql_query($sql);
//check the result
if ( $result >= 1 )
{
$message = die("That username is already in use!");
}
else
{
$message = "That username checks out<br>";
}
//output the result
return $message;
}//end of function

//Check Username
echo CheckUserNameAlreadyExists( $uname );
//The posting
$post = "INSERT INTO Users VALUES ('$fname','$lname','$uname','$upass','$email','$url','$aim','$msn','$yah','$not')";
mysql_query($post);

//Ends Connection
mysql_close();
?>


am i not supposed to use a sprintf() there?

< Message edited by Ryokotsusai -- 4/24/2006 13:40:58 >


_____________________________

The world is more like it is now than it ever has been before.
--Dwight Eisenhower

(in reply to yb2)
Ryokotsusai

 

Posts: 248
Joined: 10/5/2005
Status: offline

 
*SOLVED* Check Against Records (MySQL) - 4/24/2006 13:20:48   
*SOLVED*

ok, after playing with things and reading a few tutorials, it finaly worked!

the code right now for anyone who cares is:
<?
//Connection Settings

$sqname="*******_dotm";
$sqpass="********";
$sqdata="*******_dotm";

mysql_connect("12.34.56.78",$sqname,$sqpass);
@mysql_select_db($sqdata) or die("Unable to select database");

//Data From Html

$fname = $_POST['FN'];
$lname = $_POST['LN'];
$uname = $_POST['UN'];
$upass = $_POST['PASS'];
$cpass = $_POST['PASS2'];
$email = $_POST['EM'];

$url = $_POST['URL'];
$aim = $_POST['AIM'];
$msn = $_POST['MSN'];
$yah = $_POST['YAH'];
$not = $_POST['NOT'];

//Functions

function CheckUserNameAlreadyExists( $uname ) {
//build the sql statement
//it gets the number of times the username is in the database
//if greater than 0 then it's in there
$sql = sprintf("SELECT COUNT(*) AS test FROM Users WHERE Username = '%s'", $this->uname);
//connect to the database
$result = mysql_query($sql);
//check the result
if ( $result >= 1 )
{
$message = die("That username is already in use!");
}
else
{
$message = "That username checks out<br>";
}
//output the result
return $message;
}//end of function

//Check Username
echo CheckUserNameAlreadyExists( $uname );
//The posting
$post = "INSERT INTO Users VALUES ('$fname','$lname','$uname','$upass','$email','$url','$aim','$msn','$yah','$not')";
mysql_query($post);

//Ends Connection
mysql_close();
?>


< Message edited by Ryokotsusai -- 4/24/2006 13:40:24 >


_____________________________

The world is more like it is now than it ever has been before.
--Dwight Eisenhower

(in reply to Ryokotsusai)
yb2

 

Posts: 653
Joined: 1/30/2006
Status: offline

 
RE: *SOLVED* Check Against Records (MySQL) - 4/24/2006 13:37:28   
Glad to see it works, but if that's a real IP address in your code I'd take it out from your (public) post - no need to encourage those pesky crackers with any details about your database's existence!

_____________________________

it is natural for people not to see one's own faults, and to exaggerate other people's faults and failings.
Currently listening to: L'Enfer Des Formes by Stereolab

(in reply to Ryokotsusai)
Ryokotsusai

 

Posts: 248
Joined: 10/5/2005
Status: offline

 
RE: *SOLVED* Check Against Records (MySQL) - 4/24/2006 13:42:00   
ok, changed, and thank you for all of your help:)

_____________________________

The world is more like it is now than it ever has been before.
--Dwight Eisenhower

(in reply to yb2)
Page:   [1]

All Forums >> Web Development >> ASP and Database >> *SOLVED* Check Against Records (MySQL)
Page: [1]
Jump to: 1





New Messages No New Messages
Hot Topic w/ New Messages Hot Topic w/o New Messages
Locked w/ New Messages Locked w/o New Messages
 Post New Thread
 Reply to Message
 Post New Poll
 Submit Vote
 Delete My Own Post
 Delete My Own Thread
 Rate Posts