DyslexicDog Posted June 19, 2007 Share Posted June 19, 2007 I pretty new to PHP and OOP, but I managed to get a working object setup. I was wondering if someone could make comments on my code and let me know if I'm doing this the right way. <?php class dbtools{ //This class will connect to a defined database when called //then allow manipulation of the database. private $parameters = array ( 'username' => 'user', 'password' => 'pass', 'database' => 'data', 'server' => 'localhost' ); private $query = null; private $result = null; private $rows = null; function __construct() { //after loading default settings for database connection connect to server and // attach to database. @mysql_connect($this->parameters['server'] , $this->parameters['username'],$this->parameters['password']) or die( "unable to connect to database"); @mysql_select_db($this->parameters['database']) or die( "Unable to select database"); } public function setQuery($query) { $this->query = $query; } public function dbquery() { //this function will return a raw mysql result to be formatted externaly $this->result=mysql_query ($this->query) or die( "Unable to query database<br>".$query."<br>"); return $this->result; } public function rowCount() { $this->rows = mysql_num_rows($this->result); return $this->rows; } public function dbescape($escape) //Use this function to escape any possible SQL injection attacks //this function tests for magic quotes and removes them if it is active //then passes the incoming data to the real escape string function. { if ( get_magic_quotes_gpc()) { $escape = stripslashes($escape); } $escape = mysql_real_escape_string($escape) or die("Unable to add escape characters <br>".$escape."<br>"); return $escape; } Function __destruct() { mysql_close(); //close mysql connection. } }; ?> Quote Link to comment Share on other sites More sharing options...
Corona4456 Posted June 19, 2007 Share Posted June 19, 2007 One thing, this: die( "Unable to query database<br>".$query."<br>"); should be this: die( "Unable to query database<br>".$this->query."<br>"); Quote Link to comment Share on other sites More sharing options...
DyslexicDog Posted June 19, 2007 Author Share Posted June 19, 2007 Thanks! As far as structure goes, is this correct? I understand it can very greatly depending on who is writing the framework, but is this proper use of objects? Quote Link to comment Share on other sites More sharing options...
Corona4456 Posted June 19, 2007 Share Posted June 19, 2007 Yes, it is properly used . You might want to create the ability to use a different set of database parameters though. As it is right now you would have to modify the class directly to connect to the proper database. Just a thought . Quote Link to comment Share on other sites More sharing options...
DyslexicDog Posted June 20, 2007 Author Share Posted June 20, 2007 You might want to create the ability to use a different set of database parameters though. As it is right now you would have to modify the class directly to connect to the proper database. Just a thought . Like making the the variables public, or creating a method to update the variables? Quote Link to comment Share on other sites More sharing options...
audiokiwi Posted June 20, 2007 Share Posted June 20, 2007 Try this: function __construct($user = 'user, $password='pass', $database = 'data', $server = 'localhost') { //after loading default settings for database connection connect to server and // attach to database. @mysql_connect($server , $user,$pass) or die( "unable to connect to database"); @mysql_select_db($database) or die( "Unable to select database"); The whole $parameters array is redundant. You are using it to store default variables, but adding default values to the constructor works better. You can't do this by making the array public, because when you initialize the class, it is already connected. i.e.: $db = new dbtools(); // The database is already connected. Changing the array does nothing now $db->parameters['username'] = "New User" Quote Link to comment Share on other sites More sharing options...
Corona4456 Posted June 20, 2007 Share Posted June 20, 2007 what audiokiwi did should work just fine. Quote Link to comment Share on other sites More sharing options...
Jenk Posted June 21, 2007 Share Posted June 21, 2007 Lose the die's, your data source layer should not be directly outputting. Use exceptions to raise issues to the parent layers, so they can handle them appropriately for the circumstance. Quote Link to comment 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.