mysqli_num_rows() - 不工作

时间:2014-10-19 10:23:42

标签: php sql ajax json

我有一个表单,其中包含用户的电子邮件和密码&还有创建JSON对象的脚本,并使用AJAX将封装密码和电子邮件的对象发送到服务器:

$(document).ready(function(){
    var email, password;

    $('#submit').click(function(e){
        e.preventDefault();
        email = $('#email').val();
        password = $('#password').val();

        $memberData = {
            'email' : email,
            'password':password
        };
        $memberData = JSON.stringify($memberData);

        login();
    });

    function login()
    {  
        $.ajax({
            url: 'scripts/processLogin.php',
            data: 'json='+ $memberData,
            success: function(result){
                var r = JSON.parse(result);
                alert(r.result); 
            }
        });
    }
});

Then in my processLogin.php I have:

include 'dbConnect.php';
    $jsonS = filter_input(INPUT_GET, 'json'); //get object sent to file
    $memberData = json_decode(stripslashes($jsonS)); //convert JSON object to string
    $email = mysql_real_escape_string($memberData->email);
    $password = mysql_real_escape_string($memberData->password); 
    $query = mysql_query("SELECT * FROM `members` WHERE `email` = '$email' AND `password` = '$password'");

   $rows = mysql_num_rows($query);
   if($rows > 0){ 
        $_SESSION['email'] = $email; 
        $result = "Successfully Logged in"; 
   } else {
       $result = "Invalid Login Credentials";    
   } 
    $result = array("result"=>$result);
    echo json_encode($result);

这里是dbConnect.php:

<?php
    $dbLocation = "localhost";
    $dbName="database"; 
    $dbUsername="root";
    $dbPassword = "root";
    $connect = mysqli_connect($dbLocation,$dbUsername,$dbPassword,$dbName) 
        or die("cannot connect"); 
    mysqli_select_db($connect,$dbName)
        or die("cannot select DB");
    if (mysqli_connect_errno()) {
            echo "Failed to connect to MySQL: " . mysqli_connect_error();
        }
?>

当我运行它时,它根本不会发出警报。我尝试通过在proccessLogin.php(noob方式)中逐行添加调试并为$ result(测试缘故)分配一个普通字符串,它会发出警报,直到我添加:$ rows = mysql_num_rows($ query),我尝试使用mysqli_num_rows ()但它也没有用。我知道在我的dbConnect中我使用mysqli,但是如果我使用我的查询它不起作用,如果我使用mysqli_real_escape_string()它也不起作用 任何帮助将不胜感激。 提前谢谢。

1 个答案:

答案 0 :(得分:0)

我真的不确定我理解这个问题但是试一试。

替换这个:

 $connect = mysqli_connect($dbLocation,$dbUsername,$dbPassword,$dbName);

用这个:

 $connect = new mysqli($dbLocation,$dbUsername,$dbPassword,$dbName);

和此:

$query = mysql_query("SELECT * FROM `members` WHERE `email` = '$email' AND `password` = '$password'");

$rows = mysql_num_rows($query);

用这个:

$query = $connect->query("SELECT * FROM `members` WHERE `email` = '$email' AND `password` = '$password'");

$rows = $query->num_rows;