Jump to content

Can't close database resource after query inside a function


eelmitchell

Recommended Posts

I have a function that does a SELECT statement but when it returns and I attempt to close the db resource I get the following message:

 

Warning: mysqli_close() [function.mysqli-close]: Couldn't fetch mysqli in /homepages/13/d280102247/htdocs/GiftShop/salesform.php on line 45.

 

I open the  connection with mysqli_connect(...) The connect function assigns a value to $dbc.

 

        $dbc = mysqli_connect($server, $user, $password)

          or die('Error connecting to MySQL server.');

 

and then does a query to use my database.  After that I invoke the function:

 

        // if the form asked to list the contents

        if(isset($_POST['list']) && ($_POST['list'] == 'List')) {

            ListSales(&$dbc);

            mysqli_close($dbc);

 

Inside the function is a "SELECT * FROM sales" executed via an mysqli_query() statement.  I tried passing the database resource variable $dbc by value and as above which I assume is by reference.  Both syntax methods produce the same error message.  The line 45 is the _close(...) statement.

 

I don't know what the phrase "Couldn't fetch mysqli in ..." means in the error message.

 

If I place the mysqli_close($dbc) within the function then I get no error message.

 

I can only assume that the $dbc variable is carrying more info about the database than is visible to me and that any changes don't get transmitted outside the ListSales(...) function which would be reasonable if the $dbc were passed by value.

 

Is there  something I can do to stop the error message occurring?

Link to comment
Share on other sites

I see now that msqli is a big class.  When I tried to put together a simple test, there was a warning message:

 

Call-time pass by reference has been deprecated.

 

This is PHP 5.3.

 

I got rid of the message by removing the mysqli_close, assuming that connection will be closed for me automatically.

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.