我正在尝试在线主机上运行我的网站,但我一直收到错误

时间:2018-05-10 06:48:20

标签: php sql

每次我试图在000Webhost上运行以下PHP代码时,我都会收到此错误 - mysqli_num_rows()期望参数1为mysqli_result。

相同的代码已成功运行,我的本地主机XAMPP没有错误,我查看了很多示例,但发现这个错误是由查询中的错误引起的,但如上所述,查询完全适用于我的本地主机。

错误在代码中显示。

任何帮助都将不胜感激。

    <?php
    session_start();

    //decalre variables

    $DeviceID ="";
    $productID ="";


    //connect to database
    $db = mysqli_connect('localhost','id5655845_grocerywatch1234','123456','id5655845_grocerywatch1234');


    //validate product id and device id are avaliable

    if(isset($_POST['validate_user'])){
           $DeviceID = mysqli_real_escape_string($db,$_POST['DeviceID']);
           $productID = mysqli_real_escape_string($db,$_POST['productID']);


            $query = "SELECT * FROM configuration WHERE DeviceID='$DeviceID' AND productID='$productID'";
            $result1 = mysqli_query($db,$query);
            echo $query;

//error indicated on the following line.
            if(mysqli_num_rows($result1) == 1){
                $_SESSION['DeviceID'] = $DeviceID;
                $_SESSION['success'] = "You are now logged in";
                header('location: register.php');
            }
            else{
                echo "Device Not registered";
                echo "Product Doesnt Exist";
            }

    }

2 个答案:

答案 0 :(得分:2)

我认为您的查询很可能失败。 mysqli_query的返回值在失败时为False,否则为mysqli_result。见docs here

通过正确格式化字符串进行修复:

...
$query = "SELECT * FROM configuration WHERE DeviceID='".$DeviceID."' AND productID='".$productID."'";
$result1 = mysqli_query($db,$query);
echo $query;

if ($result1 == false){
    echo "Error has occurred!";
}
elseif (mysqli_num_rows($result1) == 1){
    $_SESSION['DeviceID'] = $DeviceID;
    $_SESSION['success'] = "You are now logged in";
    header('Location: register.php');
}
else{
    echo "Device Not registered";
    echo "Product Doesnt Exist";
}

答案 1 :(得分:0)

查询不返回任何行或是错误的,因此返回FALSE。将其更改为

if (!$dbc || mysqli_num_rows($dbc) == 0)
  

返回值

     

成功时返回TRUE,失败时返回FALSE。对于SELECT,SHOW,   DESCRIBE或EXPLAIN mysqli_query()将返回一个结果对象。