www.webdeveloper.com
Recent Articles
  • Finding Slow Running Queries in ASE 15
  • A More Advanced Pie Chart for Analysis Services Data
  • Adobe AIR Programming Unleashed: Working with Windows
  • Performance Testing SQL Server 2008's Change Data Capture Functionality
  • The ABC's of PHP: Introduction to PHP
  • How to Migrate from BasicFiles to SecureFiles Storage
  • Why the Twitter Haters Are Wrong
  • User Personalization with PHP: Beginning the Application
  • Whats in an Oracle Schema?
  • Lighting Enhancement in Photoshop
  •  

    Go Back   WebDeveloper.com > Server-Side Development > PHP

    PHP Discussion and technical support for using and deploying PHP based websites.

    Reply
     
    Thread Tools Rate Thread Display Modes
      #1  
    Old 03-08-2010, 11:45 PM
    themonkey40 themonkey40 is offline
    Registered User
     
    Join Date: Apr 2009
    Posts: 107
    OOP Class can't close connection

    I am trying to write a function that closes the connection created in the following class:

    PHP Code:
    class dbConnect {
        
        function
    connect($dataB) {
            
    $conn=mysql_connect("127.0.0.1","username","password") or die (mysql_error());
            
    $db=mysql_select_db($dataB,$conn) or die ("Unable to connect to database1");
        }

            function
    closeConnect($conn) {
            
    mysql_close($conn);
        }
    }
    to open a connection, I type:
    PHP Code:
    $connection1-> new dbConnect();
    $sql=$connection1->connect($dataB);
    I know the function closeConnect() isn't working. What will make it work?
    Reply With Quote
      #2  
    Old 03-09-2010, 03:03 AM
    Mindzai's Avatar
    Mindzai Mindzai is offline
    Registered User
     
    Join Date: Nov 2008
    Posts: 2,462
    Well, you need to call it for it to run. If you want it called automatically at the end of execution (which is not necessary btw wince PHP will close the connection for you) you could call it from a destructor method.

    Also you are assigning the result of the connect method to a variable but the method doesn't return anything.

    You might also want to consider storing the link identifier as a class property instead of passing it around as an argument to methods. Currently you could only ever call closeConnect() from within connect() since the link identifier variable only exists within the scope of that method.
    __________________
    The first rule of Tautology Club is the first rule of Tautology Club.
    Reply With Quote
      #3  
    Old 03-09-2010, 10:24 AM
    themonkey40 themonkey40 is offline
    Registered User
     
    Join Date: Apr 2009
    Posts: 107
    Mindzai could you give me an example?
    Reply With Quote
      #4  
    Old 03-09-2010, 11:55 AM
    NogDog's Avatar
    NogDog NogDog is online now
    High Energy Magic Dept.
     
    Join Date: Aug 2004
    Location: Ankh-Morpork
    Posts: 14,727
    Here's a quick (and untested) cut at how you might approach it:
    PHP Code:
    <?php
    class dbConnect
    {
       
    public $conn;
       
    private $dbHost = "127.0.0.1";
       
    private $dbUser = "username";
       
    private $dbPassword = "password";
       function
    connect($dataB)
       {
          
    $this->conn = mysql_connect($this->dbHost,
                                      
    $this->dbUser,
                                      
    $this->dbPassword);
          if (
    $this->conn == false) {
             
    throw new Exception(mysql_error());
          }
          
    $db = mysql_select_db($dataB, $conn);
          if (
    $db == false) {
             
    throw new Exception("Unable to connect to database1");
          }
       }
       function
    closeConnect()
       {
          if (!empty(
    $this->conn)) {
             
    mysql_close($this->conn);
          }
       }
       function
    __destruct()
       {
          
    $this->closeConnect();
       }
    }
    __________________
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    Kindle Minds (blog about Amazon Kindle)
    Reply With Quote
      #5  
    Old 03-09-2010, 11:58 AM
    NogDog's Avatar
    NogDog NogDog is online now
    High Energy Magic Dept.
     
    Join Date: Aug 2004
    Location: Ankh-Morpork
    Posts: 14,727
    PS: You could use the MySQLi extension in object-oriented mode and not have to create your own class.
    __________________
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    Kindle Minds (blog about Amazon Kindle)
    Reply With Quote
    Reply

    Bookmarks

    Tags
    class, close connection, function, oop


    Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
     
    Thread Tools
    Display Modes Rate This Thread
    Rate This Thread:

    Posting Rules
    You may not post new threads
    You may not post replies
    You may not post attachments
    You may not edit your posts

    BB code is On
    Smilies are On
    [IMG] code is Off
    HTML code is Off
    Forum Jump


    All times are GMT -5. The time now is 01:16 PM.



    Acceptable Use Policy

    Internet.com
    The Network for Technology Professionals

    Search:

    About Internet.com

    Legal Notices, Licensing, Permissions, Privacy Policy.
    Advertise | Newsletters | E-mail Offers

    Powered by vBulletin® Version 3.7.3
    Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.