jont Posted December 29, 2009 Share Posted December 29, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/186603-is-there-a-correct-way-to-build-a-phpmysql-class/ Share on other sites More sharing options...
abazoskib Posted December 29, 2009 Share Posted December 29, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/186603-is-there-a-correct-way-to-build-a-phpmysql-class/#findComment-985566 Share on other sites More sharing options...
ignace Posted December 29, 2009 Share Posted December 29, 2009 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' => ..)).. Quote Link to comment https://forums.phpfreaks.com/topic/186603-is-there-a-correct-way-to-build-a-phpmysql-class/#findComment-985680 Share on other sites More sharing options...
jont Posted January 1, 2010 Author Share Posted January 1, 2010 are there any disadvantages of using the way I've done it, with some changes like suggested? Quote Link to comment https://forums.phpfreaks.com/topic/186603-is-there-a-correct-way-to-build-a-phpmysql-class/#findComment-986907 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.