Jump to content

Creating A Central MySQL Interface Class


JustinK101

Recommended Posts

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

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();
    }
}
?>

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.