
Originally Posted by
wickedneat
Are you meaning like this?
PHP Code:
Class DbFunctions{
function selectadmininfo($table,$column,$condition){
$selQuery="SELECT * FROM ".$table . $condition;
$sqlQue=$this->executeQuary($selQuery);
return $sqlQue;
}
}
$database = new DbFunctions;
If you are going to go through the routine of creating a database management class, you'd ideally want to be able to use that class in as many different projects as possible which would mean leaving it as "loosely coupled" as possible - i.e. not tied to a particular implementation.
So a couple of observations:
Next time you use this class, what if you want to be more specific than "select *" ? (@kduv has shown one way round this, and has mentioned PDO too, Props for showing how to fire up PDO correctly by the way)
What if you want to use another database, with different credentials?
This line looks to have a typo which will up and bite you: "Quary"
PHP Code:
$this->executeQuary($selQuery);
Sometimes it helps to envisage how you would like your new class to appear when you call it and look at a couple of different scenarios in different projects ....
Here's a simple example:
PHP Code:
// contains $db_host etc
include 'project_a_settings.php';
// plucked from an incoming request
$fields = array(
'name' => $name,
'postcode' => $postcode,
'phone'=> $phone
);
$table = "mytable";
// do the db connection
$db = new db($db_host, $db_user, $db_pass);
// return an array of results
$db->getAll($table, $fields);
//OR
// self explanatory
//$db->getWhere($table, $fields, array('name' => 'Joe Bloggs'));
Then try and build up and out from that, you will probably change your mind as you go through this process. The function names will probably develop and change too. It would be a good idea to build a small database with some test data in it so that you can test this class in one place for when you (inevitably) come back and decide to change it, and want to test how the results compare so that you do not break all the existing code which depends on this class.
It is a good process to go through, but at the end of it you may decide that other code in existing frameworks actually do a better job and are more robust because they have had more eyes testing them - but at least you will have a slightly better appreciation of why you are doing that.
I'f nothing else I'd really urge you, as others have, to build your class around PDO though.
Bookmarks