RE: [PHP-DB] User validation problem (newbie issue)
| From: | o dot cook at etoncollege dot org dot uk | Date: | Sat, 02 Sep 2000 18:29:45 +0000 |
| Subject: | RE: [PHP-DB] User validation problem (newbie issue) | ||
| Groups: | php.db | ||
| Request: | Send a blank email to php-db+get-2586@lists.php.net to get a copy of this message | ||
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Try something like this:
$query = "SELECT * FROM users WHERE username='$username' AND
pwd='$pwd'";
$result = mysql_query($sql,$conn);
$rows = mysql_num_rows($result);
if ($rows == 0) {
// If no rows are returned, then the combination isn't correct.
$access = 0; // $access is a boolean (0 - denied; 1 - granted)
} elseif ($rows == 1) {
// If one row is returned, then the combination is correct.
$access = 1;
} elseif ($rows >= 1) {
// If more than one row is returned, we have issues!
echo "<p>More than one row returned. That's not good.</p>");
$access = 0;
}
Untested, but I'm sure you get the gist of it.
- -----------------------------------
Ollie Cook +44 (7946) 057 198
Programmer, XCalibre Communications
- -----------------------------------
> -----Original Message-----
> From: Charles Harvey [mailto:soops@threemoonsnetwork.net]
> Sent: 02 September 2000 19:22
> To: php-db@lists.php.net
> Subject: [PHP-DB] User validation problem (newbie issue)
>
>
> Please help,
>
> I am trying to puzzle out how to validate users against a
> database. In the
> attached script, if the user name & pwd are incorrect, two errors
> are returned and the else condition is met. How do I suppress
> these errors, or
> else test for them before they are echoed to the screen?
>
> //below line starts at line 8 for reference against the
> errors messages
> <?PHP
> require("connect_inc.php");
> // This query works
> $sql = "select * from users where username='$username' and
> pwd='$pwd'" or
> die ("Error in query");
> $result = mysql_query($sql) or die("invalid user or password");
>
> echo "<br>";
> // assigns retrieved data to testable variables
> $username_test = mysql_result($result,0,"username");
> $pwd_test = mysql_result($result,0,"pwd");
>
> if($username_test=$username && $pwd_test=$pwd) {
>
> echo "it worked!!";
>
> }
> else {
> echo "Sorry, it didn't work.";
> }
>
> //this releases memory (on the client or server?)
> mysql_free_result($result);
> ?>
>
> Here are the errors received if the exact query data is not
> correct: Warning: Unable to jump to row 0 on MySQL result index 2
> in
> /home/httpd/html/php/members.php on line 16
>
> Warning: Unable to jump to row 0 on MySQL result index 2 in
> /home/httpd/html/php/members.php on line 17
>
> I think the issue is how I am retrieving the username and pwd from
> the query, but I haven't been able to figure out a better way.
> Please advise a
> better way to accomplish this? Thanks!
>
> Charles Harvey
> Three Moons Network
> http://threemoonsnetwork.net
> 419-818-5260
>
>
> --
> PHP Database Mailing List (http://www.php.net/)
> To unsubscribe, e-mail: php-db-unsubscribe@lists.php.net
> For additional commands, e-mail: php-db-help@lists.php.net
> To contact the list administrators, e-mail:
> php-list-admin@lists.php.net
>
-----BEGIN PGP SIGNATURE-----
Version: PGPfreeware 6.5.8 for non-commercial use <http://www.pgp.com>
iQA/AwUBObFHGcPaWaNT0pVnEQLnpACgyeynJzvI5lVe5yYKmguVPFFbizMAoJ7T
NgJLfeZofl8ANcD9ghbY1y7R
=4vvs
-----END PGP SIGNATURE-----