Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder
    Join Date
    Jul 2007
    Posts
    100
    Thanks
    1
    Thanked 0 Times in 0 Posts

    can't close mySQL connection

    I'm trying to close my database connection at the end of each file, but I get an error when I do so:

    Warning: mysql_close(): 5 is not a valid MySQL-Link resource in C:\xampp\htdocs\website\DbConnect.class.php on line 61
    PHP Code:
    require('DbConnect.class.php');
    $db = new DbConnector;

    // Website content goes here

    $db->close(); 
    And now for the class...

    PHP Code:
    class SystemComponent
    {
        var 
    $settings;

        function 
    getSettings()
        {
            
    // System variables
            
    $settings['siteDir'] = '/public_html/website/';

            
    // Database variables
            
    $settings['dbhost'] = 'localhost';
            
    $settings['dbusername'] = 'user';
            
    $settings['dbpassword'] = 'password';
            
    $settings['dbname'] = 'database';

            return 
    $settings;
        }
    }

    class 
    DbConnector extends SystemComponent
    {
        var 
    $theQuery;
        var 
    $link;

        
    //*** Function: DbConnector, Purpose: Connect to the database ***
        
    function DbConnector()
        {
            
    // Load settings from parent class
            
    $settings SystemComponent::getSettings();

            
    // Get the main settings from the array we just loaded
            
    $host $settings['dbhost'];
            
    $db $settings['dbname'];
            
    $user $settings['dbusername'];
            
    $pass $settings['dbpassword'];

            
    // Connect to the database
            
    $this->link mysql_connect($host$user$pass);
            
    mysql_select_db($db);
            
    register_shutdown_function(array(&$this'close'));
        }

        
    //*** Function: close, Purpose: Close the connection ***
        
    function close()
        {
            
    mysql_close($this->link);
        }

    So how do I fix this?
    Last edited by Bob42; 08-05-2008 at 07:37 AM.

  • #2
    Senior Coder Len Whistler's Avatar
    Join Date
    Jul 2002
    Location
    Vancouver, BC Canada
    Posts
    1,323
    Thanks
    26
    Thanked 100 Times in 100 Posts
    I think by default that the connection closes after the query, so try leaving out $db->close();



    ---------
    Leonard Whistler

  • #3
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Its because you're resource is not open.
    I believe len is mostly right (at least this is the case in .net), if you query a recordset and the connection is closed, it will open the connection, query for data, and then close it. If the connection is previously opened, it will query for data without closing it. In PHP mysql_close is almost unnecessary since the close is called on script termination anyway.

    Consider upgrading to PHP5 object model though, it has better control of you're scopes. register shutdown functions are unnecessary since php5 has a destruct method. You can shut you're warning up by surpressing it with an @ sign in front of the mysql_close call.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #4
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,026
    Thanks
    2
    Thanked 314 Times in 306 Posts
    There are two possibilities. Either your connection failed (you have no error checking logic in your code so you will never know if the connection worked or not) or this is being caused when php is cleaning up after script execution

    For the first possibility, you should be checking if a connection was created and don't attempt to close it if there is not a connection.

    For the second possibility, when php is cleaning up at the end of the script execution, it destroys all resources used by the script, including database connection links. It then calls any functions registered using register_shutdown_function(). So, at the time your close function is called due to the shutdown function, a link would not exist anyway.
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •