##Easy to use MySQL and PostgreSQL database library for PHP.
Disclaimer: This is a heavily modified version of the database classes used in the PHP Beyond the the Basics Lynda tutorial. I am not trying to steal the original IP.
type, host, database name, user, password
// 1 for mysql
$db = new Database(1, "localhost", "foobar", "root", "toor");
// 2 for postgres
$db = new Database(2, "localhost", "foobar", "root", "toor");
class Test extends DatabaseTable {
protected static $table_name = "test";
protected static $db_fields = array(
'id',
'test'
);
protected static $db_types = array(
'int(11) NOT NULL', // id
'varchar(11) NOT NULL' // test
);
public $id;
public $test;
public static function find_by_test($database, $test){
$sql = "SELECT * FROM " . static::$table_name . " WHERE test=" . $test;
$result_array = static::find_by_sql($database, $sql);
return !empty($result_array) ? $result_array : false;
}
}
$row = Test::find_by_id($db, 1);
$rows = Test::find_by_test($db, "foobar");
$rows = Test::find_by_sql($db, "SELECT * FROM test LIMIT 3 ORDER BY ASC");
$rows = Test::find_all($db);
foreach($rows as $row){
// do stuff
}
echo $row->test;
$row->test = "foobar";
if($row->save()){
echo "yeah, it worked!";
} else {
echo "dang it";
}
if($row->delete()){
echo "yeah, it worked!";
} else {
echo "dang it";
}