-1

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

    }
  • BTW, if you're going live with that site, [Little Bobby Tables will hack you](https://stackoverflow.com/q/332365/1270789)! Use [prepared statements](https://stackoverflow.com/q/6379433/1270789)! – Ken Y-N May 10 '18 at 06:55

2 Answers2

2

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";
}
ChickenFeet
  • 2,653
  • 22
  • 26
  • Alright! Thanks, it fixes the error but for some reason it doesn't redirect to the mentioned page. – Mark sullivan May 10 '18 at 06:58
  • Sounds like a different issue. Have a try at fixing the problem yourself, if you're not sure how to fix you can always post another question. Keep in mind, it is most important on SO to _show_ that you've done the necessary work to solve the problem yourself before posting a question. – ChickenFeet May 10 '18 at 07:00
  • From examples online, I see that the redirect should look like `header('Location: example.html');`. The difference I see is the case in `Location`, your example shows a lower case `L`. – ChickenFeet May 10 '18 at 07:06
  • I have checked that part already, its the not the problem, whats happening now is that, although the information entered is valid and is stored in the database, it runs the else part, totally skips the header – Mark sullivan May 10 '18 at 07:16
  • The condition `mysqli_num_rows($result1) == 1` must be resolving as false then. There should be only 1 record, but perhaps in this case there are multiple, in which a condition of `mysqli_num_rows($result1) >= 1` can be used instead. – ChickenFeet May 10 '18 at 08:14
  • I have already resolved the issue. It was not a PHP code problem, the database on the web host was not functioning properly. – Mark sullivan May 10 '18 at 09:52
0

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.