Speedphp 数据库类常规操作CRUD

         Speedphp定义了数据表模型类后,可以在应用程序中进行对数据表的操作了。 将使用到的方法函数有create(新增), update(修改), delete(删除), find及findAll(查找),这些都是sp开发中最常用的数据库操作函数,

    1,数据库的查询find/findAll

      $conditions = array( 'name' => '小李' );
      $gb = spClass('gb'); // 初始化留言本模型类
     $result = $gb->find($conditions); // 查找
     dump($result); // 查看结果,

     请注意,find仅是返回了第一条符合条件的记录

    findAll —— 从数据表中查找记录

findAll与find的区别在于findAll是返回全部符合条件的记录,而find仅是返回findAll结果的第一条记录。

$conditions = array( 'name' => '小李' ); // 条件是同样的
$gb = spClass('gb'); // 初始化留言本模型类
$result = $gb->findAll($conditions); // 使用了findAll
dump($result); // 查看结果,   

2, create —— 在数据表中新增一行数据

$newrow = array( // PHP的数组
'name' => 'jake',
'contents' => '这是我的第一个留言',
'post_time' => date('Y-m-d H:i:s'),
'post_ip' => $_SERVER['REMOTE_ADDRESS'],
);
$gb = spClass('gb'); // 初始化留言本模型类
$gb->create($newrow);  // 进行新增操作   

3,update —— 修改数据,该函数将根据参数中设置的条件而更新表中数据。

$conditions = array('gid'=>12); // 思考为什么不能用 'name' => '小李' 来作为条件呢?
// 设置需要更新的字段,注意没必要更新的字段请不要设置。这里我们仅仅修改contents(内容)对应的数据。
$row = array('contents'=>'我的第一条记录');
$gb = spClass('gb');
$gb->update($conditions, $row);   

4,delete —— 按条件删除记录

$conditions = array('gid'=>13); // 构造条件
$gb = spClass('gb');
$gb->delete($conditions);     

总结对sql 操作就是增删改查 操作

文章来自 www.dc3688.com

猜你喜欢

转载自blog.csdn.net/gzxiaomei/article/details/82931014