JustinK101 Posted March 22, 2008 Share Posted March 22, 2008 I currently have an application where all the MySQL queries are embed into the acutal php pages. I want to pull all queries out of each page and centralize them in a file called: sql_interface.php. Furthermore, I would like to make sql_interface.php oject oriented. Basically I would like to call someting like: $db->reports->get_sales_report("2008-02-01"); $db is the parent which has sub-children such as reports, leads, calendar, settings and then each sub child has various methods and data members. Can anybody assist how I go about setting this up? Thanks for the help. Link to comment https://forums.phpfreaks.com/topic/97328-creating-a-central-mysql-interface-class/ Share on other sites More sharing options...
rofl90 Posted March 22, 2008 Share Posted March 22, 2008 Heres my database core: <?php class database { private $server; private $username; private $password; private $database; function __construct() { } public function connect($server, $username, $password, $database) { if ($server == NULL || $username == NULL || $database == NULL) { echo "Cannot connect."; exit(); } else { $this->server = $server; $this->username = $username; $this->password = $password; $this->database = $database; $this->database_connect (); } } public function error($text) { echo "There has been an error. Please try again later. <br /><br /> " .$text; exit (); } private function database_connect() { $conn = mysql_connect ( $this->server, $this->username, $this->password ); if ($conn == false) { $this->error (mysql_error()); } else { $this->select_db (); } } private function select_db() { $select_db = mysql_select_db ( $this->database ); if ($select_db == false) { $this->error (mysql_error()); } } public function query($query_text) { $query = mysql_query ( $query_text ) or die(mysql_error()); return $query; } public function fetch_array($query_object) { $fetch = mysql_fetch_array ( $query_object ); return $fetch; } public function num_rows($query_object) { $num = mysql_num_rows ( $query_object ); return $num; } function __destruct() { unset ( $this->server ); unset ( $this->username ); unset ( $this->password ); unset ( $this->database ); mysql_close(); } } ?> Link to comment https://forums.phpfreaks.com/topic/97328-creating-a-central-mysql-interface-class/#findComment-498045 Share on other sites More sharing options...
rofl90 Posted March 22, 2008 Share Posted March 22, 2008 Sorry didn't read it all, You'd have to make classes that extend it and then use that class solely or use functions within functions. Link to comment https://forums.phpfreaks.com/topic/97328-creating-a-central-mysql-interface-class/#findComment-498046 Share on other sites More sharing options...
DyslexicDog Posted March 22, 2008 Share Posted March 22, 2008 Your mysql queries should reside inside your each of your classes. Creating a central class with only sql queries doesn't really make sense IMO. Say you have a person class part of that person class pulls a record from the database say "SELECT * FROM person WHERE "id" = 17. It doesn't make sense to create a new object using your sql class just to get that information. Btw this is the wrong board you should try the design help forum or the OO help child board. Link to comment https://forums.phpfreaks.com/topic/97328-creating-a-central-mysql-interface-class/#findComment-498048 Share on other sites More sharing options...
rofl90 Posted March 22, 2008 Share Posted March 22, 2008 meh I like it its kind of redundant but can be efficient eg $db = new database; $db->query("SELECT * FROM blabla WHERE blabla='blabla'"); if($db // blabla Link to comment https://forums.phpfreaks.com/topic/97328-creating-a-central-mysql-interface-class/#findComment-498050 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.