0

Possible Duplicate:
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result

When typing in a username and password im getting the error

mysql_num_rows(): supplied argument is not a valid MySQL result resource

IT IS SAYING I HAVE NO DATABASE SELECTED ...

I was wondering what this error means. Could it be a problem connecting to the database or is it a syntax problem. here is the code for the username and password

$query = mysql_query("SELECT * FROM Users WHERE username = '$user'");
                $numrows = mysql_num_rows($query);
                if($numrows == 1){
                    $row = mysql_fetch_assoc($query);
                    $dbid = $row['id'];
                    $dbuser = $row['username'];
                    $dbpass = $row['password'];
                    $dbactive = $row['active'];

                    if($password == $dbpass){
                        if($dbactive == 1){

                            //set session info
                            $_SESSION['userid'] = $dbid;
                            $_SESSION['username'] = $dbuser;

                            echo "You have been logged in as <b>$dbuser</b> Click here to go to member page.";

                        }else
                            echo "You must activate your account to login.";

                    }else 
                        echo "You did not enter the correct password.";


                }else 
                    echo "The username you entered was not found.";
            }


        }else
            echo "You must enter your username.";

    }

The problem is in the $query line. the first line

Community
  • 1
  • 1
Waggoner_Keith
  • 590
  • 2
  • 9
  • 37

2 Answers2

1

The most likely cause is that your not connected or not selecting the correct database, you should use mysql_error() to track this down. But I also see some logic confusion, here is an example of a super simple login, checks should be made on the database query, not by PHP (plus there is no need to store the password anywhere other then in the database). You should also hash user account passwords & escape user input, hope it helps..

<?php 
session_start();
$db = mysql_connect('host','uesr','pass') or die(mysql_error());
mysql_select_db('database') or die(mysql_error());

if($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['username']) && isset($_POST['password'])){

    $username = mysql_real_escape_string($_POST['username']);
    $password = mysql_real_escape_string($_POST['password']);

    $query = mysql_query("SELECT `username`,`active`
                          FROM Users 
                          WHERE username ='$user' && password='$password' && active=1
                          LIMIT 1");

    if(mysql_num_rows($query) == 1){
        //Log in user found
        $row = mysql_fetch_assoc($query);

        $_SESSION['username']  = $row['username'];
        $_SESSION['active']    = $row['active'];
        $_SESSION['logged_in'] =true;
        header('Location: ./members.php');
        die;
    }else{
        //failed login
        $_SESSION['logged_in']=false;
        $_SESSION['log_in_error']='Fail!! Account not active or incorrect login information!';
        header('Location: ./login.php');
        die;
    }
}
?>
Lawrence Cherone
  • 46,049
  • 7
  • 62
  • 106
0

Like zerkms suggested, try printing the mysql_error() to screen. Something like:

$query = mysql_query("your_query here");
if(!$query){
    echo 'Error! : ' . mysql_error();
}

http://www.php.net/manual/en/function.mysql-error.php

Before you do all of this, make sure you are connected to a database. For example: http://php.net/manual/en/function.mysql-connect.php

$link = mysql_connect('localhost', 'database_user', 'database_password');
if(!$link){
    die('Could not connect: ' . mysql_error());
}

//select your db
mysql_select_db("YOUR_DB_NAME",$link);


$result = mysql_query("SELECT * FROM Users WHERE username = '$user'");
if(!$result){
    echo 'Error! ' . mysql_error();
}
esertbas
  • 476
  • 3
  • 7
Terry Seidler
  • 2,043
  • 15
  • 28