Jump to content

Executing database lookups then losing connection


pontifex

Recommended Posts

I've been working on a little project to learn PHP, building something nice for my friends and help me get a job.  I ran into snag at about 3am (now -_-) and I found a workaround, but I don't quite understand what's going on.

 

I've spent most of the day building a database interaction class to use with my script.  Every thing's been going well, until I started to test multiple functions together.  Code:

 

  function calc_active_users() {
    $query = 'SELECT * FROM '.ACTIVE_USER_TABLE;

    #select the schema to query from
    mysql_select_db(DB_NAME, $this->$connection) or die(mysql_error());

    #do the query and error reporting
    $result = mysql_query($query, $this->$connection) or die(mysql_error());

    $this->$active_users = mysql_numrows($result);
    mysql_free_result($result);
  }

  function calc_guests() {
    $query = 'SELECT * FROM '.GUEST_TABLE;

    #select the schema to query from
    mysql_select_db(DB_NAME, $this->$connection) or die(mysql_error());

    #do the query and error reporting
    $result = mysql_query($query, $this->$connection) or die(mysql_error());

    $this->$guests = mysql_numrows($result);
    mysql_free_result($result);
  }

 

Pretty simple.  I'm going to be keeping track of the users visiting my site and those that are logged in at the moment.  I'm going to store this information in the database and these functions retrieve some information about these tables.  I've successfully setup the tables and each function works alone.  But when executed together...

 

Warning: mysql_select_db(): supplied argument is not a valid MySQL-Link resource in /obsfucated/url/database.php on line 171

 

It doesn't matter the order I execute them in, the second one executed doesn't have a handle to the database connection.  I've declared the $connection globally to the class and have no problems accessing it.  I've read that the mysql_connect() function closes the connection after the script is completed, but here the connection seems to be closing right in the middle.  The work-around I've found that works is simply a reconnect function appended to the beginning of these two functions.  The reconnect function just refreshes the already open (?) database connection and everything works as I expect.

 

  function db_connect() {
    $this->$connection = mysql_connect(DB_SERVER, DB_USERNAME, DB_PASSWORD) or die (mysql_error());
  }

 

So I'd like to know.  Is this really necessary to call multiple functions via the object or am I doing something wrong?

 

--Pontifex

Link to comment
Share on other sites

:) by just looking this code, I can say youre doing something wrong, what I guess is youre not connecting to database becasue your using wrong syntax in your class.

 

function db_connect() {

    $this->$connection = mysql_connect(DB_SERVER, DB_USERNAME, DB_PASSWORD) or die (mysql_error());

  }

 

you cannot do this: '$this->$connection' 1 variable cannot have 2 $ on it. instead try this: '$this->connection'

Link to comment
Share on other sites

Well like I said above its been working pretty solidly.  Data base queries of both the 'set' and 'get' nature (reading and writing to the database) have been working perfectly with no errors or warnings from PHP.  Is there a module I can include, like Perl, that would enforce stricter adherence to the syntax of the language?  Because I've been using the '$this->$var' syntax all throughout my code and I have yet to see one syntax error because of it.  Here's my constructor:

 

 
function MySQL_DB() {
    #instance connection to the database
    $this->$connection = mysql_connect(DB_SERVER, DB_USERNAME, DB_PASSWORD) or die (mysql_error());

    #these are set only in their functions to avoid excess load
    #set to defaults for now
    $active_users = -1;
    $guests = -1;
    $members = -1;
  }

 

The $connection is also non-false when I test it.  Which is not to say that it's not undefined, but I have been doing some pretty complex data base interactions without errors and success reported every time except this latest error.

 

You suggestion to modify '$this->$connection' to '$this->connection' seems to have worked to resolve my problem.  But what was going on before!?

 

--Pontifex

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.