Jump to content

Is there a correct way to build a PHP/MySQL Class?


Recommended Posts

The subject says it all really but to explain a little better here's what I mean.

 

At the moment I've made a separate class for each type of query and one for the actual database connection so I have the following classes: dbConnect, dbSelect, dbInsert, dbUpdate and dbDelete.

 

I'll use my dbSelect class as an example of how im doing things currently.

 

Here's the dbSelect class, I've removed some non-important bits just to make it a bit shorter to post on here

function __construct($tables='', $columns='') 
{
$this->tables = $tables;
$this->columns = $columns;
$this->query = "SELECT $this->columns FROM $this->tables";
}

public function select()
{

if($this->where != '')
{ 
	$whereCount = count($this->where);
	$x = 0;
	foreach ($this->where as $column => $value)
	{
		$x++;
		$this->whereQuery .= $column . ' = \'' . $value . '\'';
		if($x!=$whereCount) $this->whereQuery . = ', ';
	}
	$this->query = $this->query . ' WHERE ' . $this->whereQuery; 
}

if ($this->order != '')
{ $this->query = $this->query . ' ORDER BY ' . $this->order;}

if ($this->limit != '')
{ $this->query = $this->query . ' LIMIT ' . $this->limit;}

$doQuery = mysql_query($this->query);
$row_count = '0';

$results = array();

while($row = mysql_fetch_assoc($doQuery))
{
	$row_count++;
	$results[$row_count] = $row;
}
$this->results = $results;
}

 

and this is an example of how im performing select querys

$selectSomething = new dbSelect("table_name","columns");
$selectSomething->where = array('column_name' => 'column value');
$selectSomething->limit = '1';
$selectSomething->select();

 

So basicly I'm creating a new object for every query, which for me works fine but I've got a feeling its all wrong.

 

I've only just started working with classes so I could be going about this completely wrong but i don't seem to be having any problems with it.

Have I gone about this completely the wrong way or is there a better/faster way of doing it?

 

You do not want to create a new object for every query. That is completely not the point of creating a class. Why odn't you create a class for a database object(connection and picking which database you want to use), then create methods within that class to perform different queries, and such. It's a hefty task to get all the functionality in there. You might want to check out sample code thats already been written. Don't reinvent the wheel.

Actually that idea is not so bad but I would change it a bit:

 

class SQLInsert {
    public function into($table) {
        return $this; // used for method chaining.
    }
    public function values($data) {
        return $this;
    }
}

$insert = new SQLInsert();
$insert->into('table')->values(array('id' => ..))->values(array('id' => ..))..

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.