tp框架链接数据库的基本操作

<?php
namespace Admin\Controller;
use Think\Controller; class MainController extends Controller { public function showList()
{
echo "大苹果商城";
}
public function test()
{
//数据访问
//造模型对象
$nation = D("Nation"); //查询
//$a=$nation->select();//查所有,返回关联数组
//$a=$nation->select("n001");
//$a=$nation->select("n001,n002");//select方法是通过主键值来查 //$a=$nation->find("n002");//find方法可以查一条数据 //连贯操作
//$a=$nation->where("name='苗族'")->select();//where代表条件
//$a=$nation->where("name='汉族' or name='满族'")->select();//可以写多个条件
//$a=$nation->table("Car")->select();//table可以用来切换表
//$a=$nation->table("car")->field("name")->select();//$a=$nation->field("name")->select();//field方法用于查询字段
//$a=$nation->table("car")->order("code desc")->select();//order用于排序
//$a=$nation->table("car")->limit(3,3)->select();//limit分页方法 limit(3,3)表示跳过3条取3条
//$a=$nation->table("car")->page(3,3)->select();//page(3,3)表示第3页第3条
//$a=$nation->table("car")->field("Brand,avg(Price)")->group("Brand")->select();//分组查询 :查询名为brand的不同类平均价格进行的分类
//$a=$nation->table("car")->field("Brand,avg(Price)")->group("Brand")->having("avg(price>50)")->select();//分组查询:查询名为brand的不同类平均价格大于50所进行的分类
//$a=$nation->alias('a')->field("b.Code as 'code',b.Name as 'name', a.Name as '民族'")->join("Info b on a.Code=b.Nation")->select();//条件查询
//$a=$nation->table('car')->distinct(true)->field('brand')->select();//distinct是去重查询:查询表明为car中的列明为brand去重
//$a=$nation->where("Code='n001'")->getfield("name");//getField是某一列的值(表示获取表明为nation中的列名为code=n001中的name列) //$a=$nation->count();//count表示在nation表中游几条数据
//$a=$nation->table("car")->avg(price);//avg方法表示是在car表中的列(price)的平均数
//$a=$nation->table("car")->max(price);//max方法表示在car表中的列为(price)的最大值
//$a=$nation->table('car')->min(price);//min方法表示在car表中的列为(price)的最小值
//$a=$nation->table('car')->sum(price);///sum方法表示在car表中的列为(price)的总和 //$sql="select * from info";//可以用原生态的sql语句
//$a=$nation->query($sql);//查询语句用query方法执行 //$sql="insert into nation values('n011','朝鲜族')";
//$a=$nation->execute($sql);//execute表示执行增删改
//var_dump($a); //$attr=array("Code"=>"n012","Name"=>"独龙族");//数组方式:必须是关联数组
//$a=$nation->add($attr);//add用于数组的方式添加数据
//var_dump($a);//如果成功返回1 //使用AR方式添加(实体类 数据库访问类 连接类)
//$nation->Code="n087";
//$nation->Name="俄罗斯族";
//$nation->add();
//表示在注册成功的情况下5秒钟跳转到注册页面 如果失败(把/ThinkPHP/Lrbrary/Think/Db/Lite.class.php中的237行注释了))
//$this->redirect('zhuCe',array(),5,'页面跳转中....'); }
public function zhuCe()
{
//实现两个逻辑
//1.显示注册页面2.向数据库添加内容
if(empty($_POST))
{
//显示页面
$this->show();
}
else
{
$n=D("nation");
$n->create();//自动收集表单必须有post数据 ///$n->Name="hello"; $z=$n->add();
if($z)
{
//显示页面
$this->success("添加成功","zhuCe"); }
else
{
//错误页面的默认跳转是返回前一页,通常不需要设置
$this->error("添加失败!");
}
} } public function canShu($id=)
{
//传递值:1.可以直接写
//$id=$_GET["id"];
//echo $id;
//2.可以给个形参($id=0)
echo $id; } }

报错(把/ThinkPHP/Lrbrary/Think/Db/Lite.class.php中的237行注释了)

上一篇:mongodb系列~mongodb定时删除数据


下一篇:Leetcode那点事儿