keysmash / php-easy-db
PHP的简单易用MySQL和PostgreSQL数据库库。
v1.0.2
2015-07-01 02:17 UTC
Requires
- php: >=5.3.0
This package is not auto-updated.
Last update: 2024-09-28 18:10:33 UTC
README
##PHP的简单易用MySQL和PostgreSQL数据库库。
免责声明:这是一个PHP Beyond the Basics Lynda教程中使用的数据库类的重度修改版。我并不试图窃取原始的知识产权。
使用参数创建数据库对象
类型、主机、数据库名称、用户、密码
// 1 for mysql $db = new Database(1, "localhost", "foobar", "root", "toor"); // 2 for postgres $db = new Database(2, "localhost", "foobar", "root", "toor");
创建基类并扩展DatabaseTable
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; } }
通过id获取行
$row = Test::find_by_id($db, 1);
使用自定义函数查找行(s)
$rows = Test::find_by_test($db, "foobar");
使用特定的sql获取行(s)
$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"; }