基本要素: 微擎系统数据库操作使用PDO兼容方式,参数绑定进行查询操作
(1) 数据表加上表前缀
$sql = "select * from ".tablename('users');
等价 select * from ims_users
(2)范围条件操作
① pdo_get()查询
使用: pdo_get($tablename,$condition,$fields,$limit)
$user = pdo_get('users', array('uid' => 1), array('username', 'uid'));
等价
select username ,uid from ims_users where uid =1 limit 1
② pdo_getcolumn()查询 指定查询表中获取一条记录
使用: pdo_getcolumn($tablename,$condition=array(),$fields,$limit)
$user =
pdo_getcolumn
('users', array('uid' => 1),'username',1));
等价
select username from ims_users where uid =1 limit 1
③ pdo_getall()查询 指定查询表中全部记录
使用:pdo_getall($tablename,$condition=array(),$fields=array(),$keyfields='',$orderby =array(), $limit=array())
$user = pdo_getall('users', array('status' => 1), array() , '' , array('uid','groupid') , array(1,10));
//生成的SQL等同于:SELECT * FROM ims_users WHERE status ='1' ORDER BY uid,groupid LIMIT 1, 10
②pdo_getslice()查询 指定查询表中获取一条记录
使用:pdo_getslice($tablename, $condition = array(), $limit = array(), &$total = null, $fields = array(), $keyfield = '', $orderby = array())
$user = pdo_getslice('users', array('status' => 2), array(1,10) , $total , array() , '' , array('uid','groupid'));
//生成的SQL等同于$user = SELECT * FROM ims_users WHERE status ='2' ORDER BY uid,groupid LIMIT 1,10
③pdo_fetch()查询一条记录,参数绑定传值,防止SQL注入
pdo_fetch($sql,$params=arrray())
$user = pdo_fetch(" SELECT username, uid FROM " .tablename('users')." WHERE uid =:uid LIMIT 1",array(":uid"=>1));
等价: $sql = select username,uid from ims_users where uid = 1 limit 1
$user = pdo_fetch("SELECT * FROM ".tablename('users')." WHERE username LIKE :username ",array(":username"=>"%mi%"))
等价:$sql = select * from ims_user where user_name like "%mi%";
④pdo_fetchcolumn() 返回值,字符串
pdo_fetchcolumn($sql,$params=array(),$column=int)
$user_total = pdo_fetchcolumn("SELECT count(*) FROM ".tablename('users') );
⑤pdo_fetchall()
pdo_fetchall($sql,$params=array(),$keyfield="");
$user = pdo_fetchall("SELECT username,uid FROM ".tablename('users'),array(),'uid');
(3)增,删, 改
新增一条数据
pdo_insert($tablename,$data=array(),$replace =false)
$user_data = array(
'username'=>'mizhou',
'status'=>'1')
$result = pdo_insert('users',$user_data)
更新一条数据
pdo_update($tablename,$data = array(),$condition,$glue='ADD')
$user_data = array(
'username'=>'mi')
$result = pdo_update('users',$user_data,array('id'=>2));
删除指定条件的数据
pdo_delete($tablename,$condition=array(),$glue='ADD')
$result = pdo_delete('users',arrau('username'=>'mi')
自定义SQL执行语句
pdo_query($sql,$params = array())
))
与pdo_query类似的是pdo_run
pdo_run 可一次执行多条语句,sql语句之间以;分割
例如
$sql = <<< EOF
$sql1 ;$sql2; $sql3
EOF;
pdo_run($sql);
pdo_debug()调试模式
INNODB事物表的处理逻辑
使用 pdo_begin(); pdo_commit(); pdo_rollback()