maddogandnoriko Posted April 1, 2009 Share Posted April 1, 2009 I created a class to handle retrieving and processing data from a public DB, it has 20 functions, and a Connector class to connect to my db. All works pretty well. In the public DB class I created one connector object to be used by all the functions. I now wonder if each function should create a new connector? I have a feeling this may be a real complex question and may depend on my code and such....so I guess I am looking for 'In general' answers.....I have included the connector class below. Thanks maddogandnoriko <?php //////////////////////////////////////////////////////////////////////////////////////// // Class: DbConnector // Purpose: Connect to a database, MySQL version /////////////////////////////////////////////////////////////////////////////////////// require_once 'SystemComponent.php'; 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: query, Purpose: Execute a database query *** function query($query) { $this->theQuery = $query; return mysql_query($query, $this->link); } //*** Function: getQuery, Purpose: Returns the last database query, for debugging *** function getQuery() { return $this->theQuery; } //*** Function: getNumRows, Purpose: Return row count, MySQL version *** function getNumRows($result){ return mysql_num_rows($result); } //*** Function: fetchArray, Purpose: Get array of query results *** function fetchArray($result) { return mysql_fetch_array($result, MYSQL_ASSOC); } //*** Function: close, Purpose: Close the connection *** function close() { mysql_close($this->link); } } Quote Link to comment https://forums.phpfreaks.com/topic/152105-solved-when-create-new-object/ Share on other sites More sharing options...
Maq Posted April 1, 2009 Share Posted April 1, 2009 You don't need a new connection for each function so only call the connect DbConnector() method once. Once you connect, you're connected, and should be able to invoke any method in that class successfully. Quote Link to comment https://forums.phpfreaks.com/topic/152105-solved-when-create-new-object/#findComment-798831 Share on other sites More sharing options...
maddogandnoriko Posted April 1, 2009 Author Share Posted April 1, 2009 Thank you for the advice. Sounds good to me. Less objects seemed better to me but wasn't sure. maddogandnoriko Quote Link to comment https://forums.phpfreaks.com/topic/152105-solved-when-create-new-object/#findComment-798841 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.