[PHP] 纯文本查看 复制代码 <?php
namespace Home\Controller;
use Think\Controller;
class IndexController extends Controller {
public function index(){
$m = M('User');
// 1、增:A、增加属性;B、调用add()方法插入数据库
$m->username = "Thinkphp";
$m->password = md5("123456");
// SQL语句:INSERT INTO `user` (`username`,`password`) VALUES ('Thinkphp','e10adc3949ba59abbe56e057f20f883e')
echo $m->add();
// 2、删:A、提供查询条件;B、调用delete()方法执行删除操作
// SQL语句:DELETE FROM `user` WHERE ( username="Thinkphp" )
echo $m->where('username="Thinkphp"')->delete();
// 3、改:A、定义需要修改的数据内容;B、调用save()方法进行查找更新
$data['id'] = 1;
$data['username'] = "sian";
$data['password'] = md5("123456");
// SQL语句:UPDATE `user` SET `username`='sian',`password`='e10adc3949ba59abbe56e057f20f883e' WHERE `id` = 1
$m->save($data);
// 4、查
// 查找所有,返回关联数据
// SQL语句:SELECT * FROM `user`
$array = $m->select();
// 指定查找
// SQL语句:SELECT * FROM `user` WHERE ( id=2 ) LIMIT 1
echo $m->where('id=2')->find();
// 查找某条记录的某个字段
// SQL语句:SELECT `username` FROM `user` WHERE ( id=2 ) LIMIT 1
echo $m->where('id=2')->getField('username');
}
}
|