Jump to content

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?

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.