YAOMK Posted June 3, 2007 Share Posted June 3, 2007 Hi, I'm new to php and mySQL, I'm currently developing a web application using php and mySQL as the back-end and flash as the user interface (both integrating through amfphp). I'm wondering what is the best practice to manipulate the tables in my database. Should I make a class for each table (as some articles I've read suggest) or do I make classes that contain the methods and queries necessary to carryout a particular transaction across multiple tables? Also is it a good practice to use a base class as a configuration file? I've searched for the subject without much success.Thanks in advance for your time. YAOMK Here is an example of my app thus far: <?php class Config //configuration class { protected $DB_SERVER; protected $DB_NAME; protected $DB_USER; protected $DB_USER_PASSWORD; protected $DB_AGENT; protected $DB_AGENT_PASSWORD; protected $DB_ADMIN; protected $DB_ADMIN_PASSWORD; function __construct() //set corresponding values within the quotes { $this->DB_SERVER = ""; $this->DB_NAME = ""; $this->DB_USER = ""; $this->DB_USER_PASSWORD = ""; $this->DB_AGENT = ""; $this->DB_AGENT_PASSWORD = ""; $this->DB_ADMIN = ""; $this->DB_ADMIN_PASSWORD = ""; //sets error reporting level: E_ALL, E_STRICT, E_WARNING, etc... error_reporting(E_ALL); } } ?> <?php require_once('Config.php'); require_once('c:\wamp\php\pear\DB.php'); class Database extends Config //manages all database connection using pear db. //recieves a query argument and sets either the user, admin or agent as the db user. //this class requires the pear db module and the Config class. { private $user; private $agent; private $admin; private $db; private $query; private $result; public function connect($query, $user, $agent, $admin) { $this->query = $query; $this->user = $user; $this->agent = $agent; $this->admin = $admin; if (isset($this->user) && $this->user == Config::$this->DB_USER) { $this->db = DB::connect('mysql://'.Config::$this->DB_USER.':'.Config::$this->DB_USER_PASSWORD.'@'.Config::$this->DB_SERVER.'/'.Config::$this->DB_NAME); $this->result = $this->db->query($this->query); return $this->result; exit; } elseif (isset($this->agent) && $this->agent == Config::$this->DB_AGENT) { $this->db = DB::connect('mysql://'.Config::$this->DB_USER.':'.Config::$this->DB_USER_PASSWORD.'@'.Config::$this->DB_SERVER.'/'.Config::$this->DB_NAME); $this->result = $this->db->query($this->query); return $this->result; exit; } elseif (isset($this->admin) && $this->admin == Config::$this->DB_ADMIN) { $this->db = DB::connect('mysql://'.Config::$this->DB_ADMIN.':'.Config::$this->DB_ADMIN_PASSWORD.'@'.Config::$this->DB_SERVER.'/'.Config::$this->DB_NAME); $this->result = $this->db->query($this->query); return $this->result; exit; } } } //undecided as to managing db errors in this class or the calling class. /*if(DB::isError($this->result)) { die($db->getMessage()); }*/ ?> Quote Link to comment https://forums.phpfreaks.com/topic/54065-solved-php5-oop-and-mysql/ Share on other sites More sharing options...
fenway Posted June 6, 2007 Share Posted June 6, 2007 It seems to hard to believe that such an object-based implementation doesn't already exist. Quote Link to comment https://forums.phpfreaks.com/topic/54065-solved-php5-oop-and-mysql/#findComment-269111 Share on other sites More sharing options...
YAOMK Posted June 6, 2007 Author Share Posted June 6, 2007 Hi Fenway, Thanks for your reply. I'm with you. I'm just concerned about best practices. I wanted to know if this is an efficient way doing it. Also how should I deal with my tables? a class with methods for each one? or logical classes that run queries across multiple tables? I'm very new to php and mysql, so I'm not confident on my php programming. I guess my question is; how would you do it? Quote Link to comment https://forums.phpfreaks.com/topic/54065-solved-php5-oop-and-mysql/#findComment-269145 Share on other sites More sharing options...
fenway Posted June 6, 2007 Share Posted June 6, 2007 Don't split by table, it's not a logical separation compared to different query types (select, count, update,insert, delete, etc). Quote Link to comment https://forums.phpfreaks.com/topic/54065-solved-php5-oop-and-mysql/#findComment-269408 Share on other sites More sharing options...
YAOMK Posted June 6, 2007 Author Share Posted June 6, 2007 Cool, now I can go on write my classes. Many thanks Fenway. Quote Link to comment https://forums.phpfreaks.com/topic/54065-solved-php5-oop-and-mysql/#findComment-269449 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.