mysql_num_rows()导致警告

时间:2013-03-23 13:35:54

标签: php html mysql

我遇到mysql_num_rows问题。当我尝试登录时,会出现以下警告:

警告:mysql_num_rows()期望参数1是资源,在路径/ / userClass.php中给出的对象#no。

我搜索了那个错误,并且得到了它,这是因为错误的sql查询引起的。但我不确定。请帮忙。

Userclass.php

登录功能

$sql = "SELECT id, username FROM `user` WHERE username = '$username' AND hashed_password ='$password'";

$result = mysqli_query( $this->con, $sql );
if ( !$result ) die ("Database query failed" . mysql_error());
else{
    $count = mysql_num_rows( $result );
        if( $count == 1 ){
            $found_user = mysql_fetch_array( $result );
            $_SESSION['user_id'] = $found_user['id'];
            $_SESSION['username'] = $found_user['username'];    
            header( "Location:{$location}" );
        }else{ 
            //username/password combo was not found in the database
            $this->pass_notverified = "Username/password incorrect.";                       
            return;
        }
}

的header.php

<?php require_once 'class/userClass.php';?>
<?php $obj = new userClass;?> 

<?php 
if( isset( $_POST['login'] ) ){
    $obj->login( $_POST,"home.php" );
}?>

2 个答案:

答案 0 :(得分:2)

简单的解决方案:

$result = mysqli_query( $this->con, $sql );

$count = mysql_num_rows( $result );
$found_user = mysql_fetch_array( $result );
不要一起工作。 使用$ count和$ found_user的mysqli函数(你可以找到它们here

答案 1 :(得分:0)

使用mysqli_num_rows代替mysql_num_rowsmysqli_fetch_array代替mysql_fectch_array

你不能使用mysql和mysqli功能的组合