kaduamaral / connectionpdo
ConnectionPDO 是一个数据库连接类
1.1.1
2018-05-22 01:25 UTC
Requires
- php: >=5.5.33
This package is auto-updated.
Last update: 2024-09-13 20:16:20 UTC
README
ConnectionPDO 是一个用于以更实用方式管理数据库的 PHP 类。
##版本
1.1.0
方法
示例
连接
$host = 'localhost'; $user = 'root'; $pass = ''; $base = 'test'; $con = new ConnectionPDO($host,$user,$pass,$base);
DROP
$con->drop('tab_teste');
CREATE
$fields = Array( 'id' => Array( 'type' => 'int', 'size' => '4', 'comment' => 'first key' ), 'name' => Array( 'type' => 'varchar', 'size' => '60', 'comment' => 'test name' ), 'col3' => Array( 'type' => 'varchar', 'size' => '60', 'default' => NULL, 'comment' => 'test name' ) ); $con->create('tab_teste',$fields,'id','InnoDB',false);
第五个参数用于删除表,如果存在的话。
INSERT
$data = Array('id'=>1,'name' => 'First Record', 'col3' => 'test '); $con->insert('tab_teste',$data); $data = Array('id'=>2,'name' => 'Second Record', 'col3' => 'test '); $con->insert('tab_teste',$data); $data = Array('id'=>3,'name' => 'Third Record', 'col3' => 'test '); $con->insert('tab_teste',$data);
DELETE
$where = Array('id'=>1); $con->delete('tab_teste', $where);
UPDATE
$data = Array( 'name' => 'Now this is the first record', 'col3' => 'First record' ); $where = Array('id'=>2); $con->update('tab_teste',$data, $where);
SELECT
$where = Array('id' => Array('BETWEEN'=>Array(2,3))); $res = $con->select('tab_teste',$where); $tab = $res->fetch(PDO::FETCH_ASSOC);
选择结果
------------------------------------------------------
| id | name | col3 |
------------------------------------------------------
| 2 | Now this is the first record | First record |
------------------------------------------------------
| 3 | Third Record | test |
------------------------------------------------------
WHERE
一些 $where
参数的示例
$where = 'id = 1'; // Resultado: id = 1 $where = Array('id' => 1); // Resultado: id = 1 $where = Array('id' => 1,'$OR1'=>'OR','col3' => 'test'); // Resultado: id = 1 OR col3 = 'test' $where - Array('col3' => array('LIKE' => 'recor')) // Resultado: col3 LIKE '%recor%' $where = Array('id' => Array(1,'>>>',10, Array(3,6,8))); // Resultado: id IN (1,2,4,5,7,9,10) $where = Array('id' => Array('BETWEEN' => Array(1,10))); // Resultado: id BETWEEN 1 AND 10 $where = Array('id' => Array('NOT' => Array(1,2,3,12,45))); // Resultado: id NOT IN (1,2,3,12,45) $where = Array('id' => Array('NOT' => Array(1,'>>>',10, Array(3,6,8)))); // Resultado: id NOT IN (1,2,4,5,7,9,10) $where = Array( 'id' => array('NOT' => array(1,'>>>',6,array(3,5))), '$OR'=>'OR', 'col3' => array('LIKE' => 'recor') ); // Resultado: id NOT IN (1, 2, 4, 6) OR col3 LIKE '%recor%'
要插入列之间的内容,只需传递一个以 $
开头的任何 字符串 参数。
$where = Array('id'=>3, '$a'=>'OR', 'name'=>Array('LIKE'=>'first'))
也可以传递操作符如 >
、>=
、<=
、<
,用于数值类型的列。只需给出列名和操作符,如 'coluna >' => 'valor'
$where = Array('id >'=>1) // Resultado: id > 1
另一个示例
$where = Array( 'id >' => 5, '$1'=>'OR', '$2'=>'(', 'col3' => array('LIKE' => 'recor'), '$3'=>'OR', 'name' => array('LIKE' => 'Recor'), '$4'=>')' ); // Resultado: `id` > 5 OR ( `col3` LIKE '%recor%' OR `name` LIKE '%Recor%' )
许可证
MIT 许可证下分发