简体   繁体   中英

I am trying to run my website on an online host but i keep getting an error

Every time i am trying to run the following PHP code on 000Webhost, i keep getting this error -- mysqli_num_rows() expects parameter 1 to be mysqli_result.

The same code had been run successfully without errors on my localhost, XAMPP, i have looked through many examples and only found out that this error is caused by an error in the query, but as mentioned, the query works perfectly on my localhost.

The error is indicated in the code.

Any help would be appreciated.

    <?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";
            }

    }

I think your query is likely failing. The return value for mysqli_query is False on failure, otherwise it is mysqli_result . See docs here

Fix by properly formatting string:

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

The query either returned no rows or is erroneus, thus FALSE is returned. Change it to

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

Return Values

Returns TRUE on success or FALSE on failure. For SELECT, SHOW, DESCRIBE or EXPLAIN mysqli_query() will return a result object.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM