How to properly use mysqli_free_result function in PHP-ThrowExceptions

Exception or error:

I’m trying to use mysqli_free_result function in my website, to free results after they are used.

But it says:

Warning: mysqli_fetch_assoc(): Couldn’t fetch mysqli_result in

It might also be called before I use the result. I’d like to know how to properly use it

<?php

function find_all_subjects(){
    global $connection;

    $query  = "SELECT * ";
    $query .= "FROM blog_subject WHERE ID > 2";
    $subject_set = mysqli_query($connection, $query);

    mysqli_free_result($subject_set);
    return $subject_set;
}

?>

I return value, and echo the function to display list of subjects.
Okay, if i have to use mysqli_free_result, after the return $subject_set,
Then i do i know that, the result is freed, if value has already been returned?

How to solve:

Do not use it.

function find_all_subjects($connection){

    $query  = "SELECT * FROM blog_subject WHERE ID > 2";
    return $connection->query($query)->fetch_all();
}

I’m trying to use mysqli_free_result function in my website, to free results after they are used.

No need to do it. PHP will clean everything up automatically.

Leave a Reply

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