
March 21st, 2004, 08:43 PM
|
|
Registered User
|
|
Join Date: Mar 2004
Posts: 3
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
|
PHP/mySQL Login Page
This is my first time to the forum so hopefully my code is clearly visible.
I am new to PHP, but as far as I can tell my code should be working properly. The problem is that I keep getting a error:
Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/oddprod/public_html/login.php on line 42.
As far as I can tell there is nothing wrong with that.
PHP Code:
//check that the user is calling the page from the login form and not accessing it directly //and redirect back to the login form if necessary if (!isset($username) || !isset($password)) { header( "Location: URL" ); } //check that the form fields are not empty, and redirect back to the login page if they are elseif (empty($username) || empty($password)) { header( "Location: URL" ); } else{ //convert the field values to simple variables //add slashes to the username and md5() the password $user = addslashes($_POST['username']); $pass = md5($_POST['password']);
// Connect to DB $db = mysql_connect ("localhost", "username", "password") or die ('I cannot connect to the database because: ' . mysql_error());
$db_selected = mysql_select_db('oddprod_managment', $db); if (!$db_selected) { die ('Can\'t use oddprod_managment : ' . mysql_error()); } // making sure the that the information from the form exists in the DB $result=mysql_query("select * from users where username='$usersLOGIN' AND password='$usersPASS'", $db); // check that at least one row was returned $rownum = mysql_num_rows($result); if($rownum > 0){ while($row = mysql_fetch_array($result)){ //start the session and register a variable session_start(); session_register('user');
//successful login code will go here... echo 'Success!';
//we will redirect the user to another page where we will make sure they're logged in header( "Location: index.php" ); } } else {
//if nothing is returned by the query, unsuccessful login code goes here... echo 'Incorrect login name or password. Please try again.'; } }
mysql_close($db);
|