0

可能重复:
警告:mysql_fetch_array():提供的参数不是有效的 MySQL 结果

输入用户名和密码时出现错误

mysql_num_rows():提供的参数不是有效的 MySQL 结果资源

它是说我没有选择数据库...

我想知道这个错误是什么意思。可能是连接到数据库的问题还是语法问题。这是用户名和密码的代码

$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.";

    }

问题出在 $query 行中。第一行

4

2 回答 2

1

最可能的原因是您没有连接或没有选择正确的数据库,您应该使用它mysql_error()来跟踪。我也看到一些逻辑混乱,这是一个超级简单登录的示例,应该对数据库查询进行检查,而不是通过 PHP(另外,不需要将密码存储在数据库中的任何地方)。您还应该散列用户帐户密码并转义用户输入,希望对您有所帮助..

<?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;
    }
}
?>
于 2012-04-22T21:14:11.123 回答
0

像 zerkms 建议的那样,尝试将 mysql_error() 打印到屏幕上。就像是:

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

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

在执行所有这些操作之前,请确保您已连接到数据库。例如: 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();
}
于 2012-04-22T20:59:48.113 回答