In addition to my post in this thread I've decided to post an extended example, which also implements functions for inserts and updates. I hope the code is straightforward enough. If not, reply and let me know.
The table structure I've used for my test is the following:
CREATE TABLE `contacts` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`username` varchar(64) NOT NULL,
`password` varchar(64) NOT NULL,
`first_name` varchar(64) DEFAULT NULL,
`last_name` varchar(64) DEFAULT NULL,
`email` varchar(64) NOT NULL,
PRIMARY KEY (`id`)
)
And finally, here is how to use it:
<?php
# Definition of my table structure
$tableConfig = array (
'contacts' => array (
'id' => PDO::PARAM_INT,
'username' => PDO::PARAM_STR,
'password' => PDO::PARAM_STR,
'first_name' => PDO::PARAM_STR,
'last_name' => PDO::PARAM_STR,
'email' => PDO::PARAM_STR
)
);
# Create a class instance
include 'MyDB.class.php';
$myDB = new MyDB($tableConfig);
# Create a data array for insert
$newContact = array (
'username' => 'pritaeas',
'password' => 'XXX',
'first_name' => 'Hans'
);
$id = $myDB->insert('contacts', $newContact);
# Create a data array for an update
$updContact = array (
'last_name' => 'Pollaerts',
'email' => 'pritaeas@example.com'
);
$myDB->update('contacts', $updContact, array ('id' => $id));
# Show the recently inserted and updated record
print_r($myDB->getRecord('contacts', array ('id' => $id)));
?>
Finally: a lot of room for improvement. Sanity checks, error checking, better/more where conditions, defining joins, extend configuration to allow table creation, and a lot more... I'll leave that to you.