1. Home
  2. Docs
  3. 开发文档
  4. 数据库
  5. 数据库基本操作

来客系统数据库操作使用 PDO 兼容方式,以参数绑定的形式进行查询操作。
系统已对 PDO 兼容性进行检测及封装。下面具体说明一下数据库的几种基本操作方法。

$db=DBAction::getInstance();

获取数据库连接实例,其中DBAction是数据库操作封装类。

$sql = "select * from lkt_user";
$userlist = $db->select($sql);
foreach($userlist as $key => $value)
{
 echo $value->id; 
}

select( )方法是获取表中数据,以数组形式返回。

$sql = "select * from lkt_user where id=1";
$user = $db->selectOne($sql);
echo $user->id;

selectOne( )方法是获取表中唯一数据,以对象形式返回。

$sql = "insert into lkt_record (user_id,event,type) values ('110','没事打打码',0)";
$rs = $db->insert($sql);
echo $rs;

insert( )方法是插入数据,返回影响行数。

$sql = "update lkt_user name='ketter' where id=1 ";
$rs = $db->update($sql);
echo $rs;

update( )方法是更新数据,当然也可以执行删除语句,返回影响行数。

$sql = "insert into lkt_product_list(product_title,subtitle,product_class,brand_id,weight,imgurl,content,num,s_type,add_date,volume,freight,initial,status,sort) " .
            "values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
$data = array();
$data[] = $product_title;
$data[] = $subtitle;
$data[] = $product_class;
$data[] = $brand_id;
$data[] = $weight;
$data[] = $image;
$data[] = $content;
$data[] = $z_num;
$data[] = $type;
$data[] =  date("Y-m-d H:i:s");
$data[] = $volume;
$data[] = $freight;
$data[] = $initial;
$data[] = 2;
$data[] = 0;
$id = $db->preInsert($sql,$data);
echo $id;

preInsert( )方法是预处理插入数据,返回新插入数据的主键ID。

// 根据产品id,修改产品信息
$sql = "update lkt_product_list set product_title=?,product_class=?,brand_id =?,weight=?,s_type=?,num=?,content=?,imgurl=?,subtitle=?,volume=?,freight=?,initial=?,sort=? where id = ? ";
$data = array();
$data[] = $product_title;
$data[] = $product_class;
$data[] = $brand_id;
$data[] = $weight;
$data[] = $type;
$data[] = $z_num;
$data[] = $content;
$data[] = $image;
$data[] = $subtitle;
$data[] = $volume;
$data[] = $freight;
$data[] = $initial;
$data[] = $sort;
$data[] = $id;
$rs = $db->preUpdate($sql,$data);
echo $rs;

preUpdate( )方法是预处理更新语句,返回影响的行数。

$db->begin();//开启事务
$db->rollback();//事务回滚
$db->commit();//事务提交

上述是事务中的三个方法,以飨观众。

Was this article helpful to you? Yes 1 No 1

How can we help?