php – mysqli_free_result() expects parameter 1 to be mysqli_result, null given in-ThrowExceptions

Exception or error:

I just migrated a site from one domain to another domain (and another host). I made sure that all links were replaced and exported/imported my database. The migration seemed to have worked, but for some reason I am getting an error on my new domain that I did not get on my old domain (with, as far as I know, the same code).

My error is: “Warning: mysqli_free_result() expects parameter 1 to be mysqli_result, null given in path on line 84″. I looked at the other StackOverflow questions that address this error, but I have not found a solution yet.

This is my code:

<?php
session_start();

// 1. Create a database connection
$dbhost = 
$dbuser = 
$dbpass = 
$dbname = 
$connection = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);
// Test if connection occurred.
if(mysqli_connect_errno()) {
  die("Database connection failed: " . 
    mysqli_connect_error() .
    " (" . mysqli_connect_errno() . ")"
    );
}

// 2. Perform database query
if (empty($_SESSION['order'])) {
    $query = "INSERT INTO `orders` (`order_id`) VALUES (NULL)";
    $result = mysqli_query($connection, $query);
    // Test if there was a query error
    if (!$result) {
    die("Database query failed.");
    }
    // 3. Use returned data (if any)
    $order_id_recent = mysqli_insert_id($connection);
    $_SESSION['order'] = $order_id_recent;
}

$size = $_POST["size"];
$paper = $_POST["paper"];
$type = $_POST["type"];
$quantity = $_POST["quantity"];

// 2. Perform database query
$query2 = "SELECT product_id FROM product WHERE product_type = '$type' AND size = '$size' AND paper = '$paper'";
$result2 = mysqli_query($connection, $query2);
// Test if there was a query error
if (!$result2) {
  die("Database query failed.");
}

// 3. Use returned data (if any)
while($row = mysqli_fetch_assoc($result2)) {
    $product_id = $row['product_id'];
}

$order_id = $_SESSION['order'];

// 2. Perform database query
$order_id = $_SESSION['order'];

$query3 = "SELECT * FROM order_item WHERE order_id = '$order_id' AND product_id = '$product_id'";
$result3 = mysqli_query($connection, $query3);
// Test if there was a query error
if (!$result3) {
  die("Database query failed.");
}

while($row = mysqli_fetch_assoc($result3)) { 
    $itemexistrows = mysqli_num_rows($result3);
}

if ($itemexistrows > 0) {
    $query4 = "UPDATE order_item SET quantity = quantity + '$quantity' WHERE product_id = '$product_id' AND order_id = '$order_id'";
    $result4 = mysqli_query($connection, $query4);
    if (!$result4) {
    die("Database query failed.");
    } else {
        echo 'The item has been added to your cart. <a class="text-red" href="viewcart.php">View your cart</a></div>.'; 
    }
} else {
    $query5 = "INSERT INTO `order_item`(`order_item_id`, `product_id`, `quantity`,`order_id`) VALUES (NULL,'$product_id','$quantity','$order_id')";
    $result5 = mysqli_query($connection, $query5);
    if (!$result5) {
    die("Database query failed.");
    } else {
        echo 'The item has been added to your cart. <a class="text-red" href="viewcart.php">View your cart</a></div>.'; 
    }
}

// 4. Release returned data
mysqli_free_result($result);

// 5. Close database connection
mysqli_close($connection);
?>

The weird thing is that my website still seems to work. These lines of code are part of a shopping cart module and the shopping cart seems to get updated.

How to solve:

I suspect that $_SESSION['order'] is not empty and thus $result is not set as it does not seem to be addressed inside the same if statement.

The following would almost certainly make the problem go away.

 if(isset($result) && $result!=null){
     // 4. Release returned data
     mysqli_free_result($result);
 }

Or, as suggested in comments, and probably better:

 if(isset($result) && is_resource($result)){
     // 4. Release returned data
     mysqli_free_result($result);
 }

Leave a Reply

Your email address will not be published. Required fields are marked *