Jump to content

[SOLVED] php5 OOP and MySQL


YAOMK

Recommended Posts

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

        }*/

?>

Link to comment
https://forums.phpfreaks.com/topic/54065-solved-php5-oop-and-mysql/
Share on other sites

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?

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.