Jump to content

Database Resource Reference to Class __construct method


objnoob

Recommended Posts

Can I not pass references into class properities from outside the object?  :shrug:

 

class db_manipulation { 

private $connection = NULL;

function __construct(&$database_connection) {
$this->connection = $database_connection;
if (!$this->connection) die('couldnt connection to database');
}

function __destruct() {
mysqli_close($this->connection);
}
}

 

Results in: Warning: mysqli_close() [function.mysqli-close]: Couldn't fetch mysqli in

Link to comment
Share on other sites

A) Your code works for me,

 

B) You don't need to use a reference in this case,

 

C) Are you sure your mysqli connection worked? Your code just tests if you have a value, not if it is a valid connection.

 

D) Showing your complete code that produces (reproduces) the error is always the fastest way of getting a solution.

Link to comment
Share on other sites

A) I'm using PHP v 5.1.6.

 

B) I've tried without a reference as well, and all other database interaction methods of this class using mysqli_query, etc. execute fine.

 

C) I can use other methods that query, insert, etc just fine, so the connection is active.

 

D) The code is massive. The only other piece of code used to create the error is the instantiation of the object which is

 

$adminDB = new db_manipulation($mysqli_con);

 

I just can't seem to get the connection to close on __destruct without the warning.

 

I've tried getting PHP upgraded, but we're using RHEL5, and our Linux guy doesn't want to install anything not released through RPM which is somewhat understandable.

 

Thanks for the responses.

 

 

Link to comment
Share on other sites

$adminDB = new db_manipulation(ConnClass $mysqli_con);

 

I recently had something similar to this, and if your connection handle is generated from a class, and your trying to pass a class method into another class, you need to reference the Db class name into the newly instantiated class - as I show in the snippet there.

 

but I agree with @PFMaBiSmAd, there doesn't seem to be anything wrong with the class as you supplied, but there is no need to pass by reference, unless you are trying to save a bit of overhead?? (apologies if I read this wrong - long day at the office)

 

I'm not sure why your doing a _close in your __destruct either as the 'natural' action is for the connection to be closed at the end of the scripts execution - I quote from the manual.

 

Rw

 

Link to comment
Share on other sites

For the specific error message you posted, the only way I could produce the same is if $this->connection was a valid mysqli connection, but had already been closed by the time your __destruct() function was called.

 

Without further details of your actual code, it's not possible to directly help further.

 

Edit: I'm going to take a wild guess that you are also passing the mysqli connection into ANOTHER class(es) and one of them is closing the connection when your script ends as well, so you get the posted error in your db_manipulation class?

 

If so, you should not be manipulating the mysqli connection in a class that is just using that connection. You should only manipulate/close the connection in the scope that opened the connection.

 

 

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.