|
-
Mar 26th, 2002, 05:46 AM
#1
Thread Starter
Fanatic Member
Error handling with a DB query
i have a query checking my DB to see if a user exists (for my login script); i'm using the count(*) function in MySQL to display a '1' if the user exists but it cant return a '0' when it doesnt find anything, anyway, here is the code i am using:
PHP Code:
mysql_result(mysql_query("SELECT *, count(*) FROM users
WHERE uname='$u_name' AND upassword='$u_password'
GROUP BY uname"),0,"count(*)");
Anyone know how to handle this error ?
-
Mar 26th, 2002, 09:39 AM
#2
PowerPoster
Might be easier to return the records where the username matches then count the records returned with the mysql_num_rows function
-
Mar 26th, 2002, 10:30 AM
#3
I agree with chris on this one. don't make it harder than it is.
-
Mar 27th, 2002, 09:13 AM
#4
Addicted Member
you could do the query with the "or die" included... like so...
$sql1="select yadda.yadda from datta";
$imageresult=mysql_query($sql1)
or die ("An error has occured. <script language='javascript'>alert('uh oh, what did you do now');</script> ");
-
Mar 27th, 2002, 10:55 AM
#5
that "or die" is only if the it can't query the database. he waqnts it to return a "0" if he doesn't find anything.
-
Mar 28th, 2002, 03:44 AM
#6
Thread Starter
Fanatic Member
Originally posted by chrisjk
Might be easier to return the records where the username matches then count the records returned with the mysql_num_rows function
hmmm, sounds good "chrisjk", i think i sorta know what u mean but not totally, could u explain a little better pls ? 
btw. "TheGoldenShogun", i'v tried a "OR DIE" thiny, doesnt solve my problem, as scoutt sais, i think its only for a seruious problem like not being able to actually access the DB. (i think i know what i mean by that )
-
Mar 28th, 2002, 10:06 AM
#7
PowerPoster
okay, here's an example
PHP Code:
// connect to database
$db = mysql_connect("localhost", "username", "password");
mysql_select_db("databasename",$db);
// Formulate the query
$sql = "SELECT * FROM users WHERE uname ='$u_name'";
// Execute the query and put results in $result
$result = mysql_query($sql);
// Get number of rows in $result.
$num = mysql_numrows($result);
if ( $num == 1 ) {
// 1 record, user exists. At this point you should check the password matches
} else {
// more than 1, or doesn't exist
}
My name is chris BTW
-
Mar 28th, 2002, 01:16 PM
#8
or
PHP Code:
// connect to database
$db = mysql_connect("localhost", "username", "password");
mysql_select_db("databasename",$db);
// Formulate the query
$sql = "SELECT * FROM users WHERE uname ='$u_name'";
// Execute the query and put results in $result
$result = mysql_query($sql);
// Get number of rows in $result.
$num = mysql_numrows($result);
if ( $num == 0 ) {
// say they don't exist and do what you want
} else {
// if a record exists. then check for the password and then run rest of the page. so you can query the DB from here to check.
}
-
Mar 28th, 2002, 10:02 PM
#9
Thread Starter
Fanatic Member
ok, thanx Scoutt & *ahem* Chris
maybe i'll buy u 2 a beer @ the end of the year when i turn 18, then maybe air mail it to you, ****e, u don like flat beer do ya ? 
thanx again
-
Mar 28th, 2002, 10:28 PM
#10
PowerPoster
no worries
if it's beer, I don't mind how it gets here
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|