Click to See Complete Forum and Search --> : php/mysql login .. no duplicate entires..


bdg1983
08-02-2001, 03:15 PM
Alright.
I'm making a login for people to use on my forum. Basically everything works except this:



function in_use($userid) {
global $user_tablename;

$query = "SELECT userid FROM $user_tablename WHERE userid = '$userid'";
$result = mysql_query($query);
if(!mysql_num_rows($result)) return 0;
else return 1;
}


Now, all that should do is tell me if the userid(the registered name) is already there. IT doesn't work for some reason, error is:

Warning: Supplied argument is not a valid MySQL result resource in /home2/naqel/gaffle-www/login/register.php on line 14

Line 14 being:
if(!mysql_num_rows($result)) return 0;
else return 1;


Now, something is working somewhere, because it won't input the duplicate entry into the database, but it will go on to the next page and say congrats, you've been registered.
Help. :\

Thanks.
Alex

Stuka
08-02-2001, 04:41 PM
It won't do the duplicate entry because mysql_num_rows($result) is false. This isn't because the userid is taken, though. It's because the query failed somehow. Try putting || die("Query Error") after your mysql_query() call. It should cause the code to die there - now why it's dyin' I dunno, it might have to do with your table structure.

bdg1983
08-02-2001, 05:03 PM
uh duh. i'm a freaking retard.
i never defined what $user_tablename was.
duh.
it works fine now. :)
thanks.

Alex

Sweede
08-02-2001, 05:30 PM
good coding practice, every EVERY EVERY SQL call in php should be as..

$query = "SQL Stuff";
$result = mysql_query($query) or die("there was an error on ".__FILE__.":".__LINE__." > ".mysql_error()."<br>");

if there is an error, it will produce

there was an error on file.php:43 > "SQL Stuff isnt valid SQL you dumbass"

or whatever error pops up.

Salmon
08-02-2001, 06:34 PM
Of course, this doesn't have anything to do with your error, but you might consider this approach instead . . .


function in_use($userid) {

global $user_tablename;

// COUNT(*) is a more efficient and faster way to accomplish your task
$query = "SELECT COUNT(*) FROM $user_tablename WHERE userid = '$userid'";

$result = mysql_query($query);

return mysql_result($result, 0) ? 0 : 1;
}


That will be faster (and safer if something has gone wrong and you end up selecting *a lot* of rows)

[ 02 August 2001: Message edited by: Salmon ]

Salmon
08-02-2001, 06:43 PM
Originally posted by Sweede:
good coding practice, every EVERY EVERY SQL call in php should be as..

$query = "SQL Stuff";
$result = mysql_query($query) or die("there was an error on ".__FILE__.":".__LINE__." > ".mysql_error()."<br>");


That is *not* good coding practice. Good coding practice would test for a good query and then handle the failure gracefully. If you have some kind of debug mode enabled for your script then fine, call die() in your debug mode (nicely formatted message by the way). Otherwise, go about things a different way.

Sweede
08-03-2001, 02:50 AM
ya, that was a quicky, my error_msg() handler has a lot of thins with it.

but, usually SQl stuff, for me, is fairly important and if it fails there's no reason for it to continue.

but then, i dont write SQL to where it will fail.

i put the above causs its obvious guy is learning php and doesnt know that kinda nifty stuff yet (which i use built in php error handlers and everything fun like that)

Salmon
08-03-2001, 09:05 AM
[QUOTE]
but, usually SQl stuff, for me, is fairly important and if it fails there's no reason for it to continue.
[/QUOTE}

And in that case then calling die() would probably be OK. My point was only this:

Say you're writing a script, say, a polling application, and the query fails for reasons not related to your SQL syntax (permissions issues, user configuration errors, etc). You don't know who is going to be using it (there's alot of monkeys out there that, incorrectly, think they know what they're doing), and you don't know the circumstances under which it will be used. Do you really want to kill the page/script just because of an query error? Probably not.

[ 03 August 2001: Message edited by: Salmon ]

Stuka
08-03-2001, 12:56 PM
Salmon -
Completely correct - die() is just useful in debugging - but very useful there, IMO. Especially if you remember all the nice formatting like Sweede does (and I'll hafta read more on PHP, since I didn't yet realize it had __FILE__ and __LINE__).