您的位置:首页 > 编程语言 > PHP开发

TP框架---thinkphp查询和添加数据

2017-03-12 16:57 330 查看
查询

<?php
namespace Admin\Controller;
use Think\Controller;
class MainController extends Controller
{
public function showList()
{
echo "大苹果商城";
}

public function test()
{
//数据访问
//造模型对象
//$nation = D("Nation");//连接数据库中的Nation表

//查询
//$a = $nation->select();//查询所有,返回关联数组,也是二维数组。
//$a = $nation->select("n001,n002,n003");//通过主键值查,注意写法。

//$a = $nation->find();//查一条数据,返回一维数组。
//$a = $nation->find("n001");//按照条件查询

//$a = $nation->where("name='汉族'or name='回族'")->select();//where()只能写查询条件。select才会输出,select返回的是二维数组。这种操作称为连贯操作

//$a = $nation->table("info")->select();//table切换其它表格查询表格信息。上面虽然是链接的Nation表,这里切换到info表格。

//$a = $nation->field("code")->select();//查询指定字段,这里查询的是code列,多个字段中间用,分隔。

//$a = $nation->order("code desc")->select();//code按照降序排列

//$a = $nation->limit(3)->select();//查询前3条数据,limit分页查询。
//$a = $nation->limit(3,4)->select();//跳过3条显示4条

//$a = $nation->page(2,3)->select();//page中第一个参数是第几页,第二个参数是几条数据。这里是取第2页的3条数据。

//$a = $nation->table("car")->field("Brand,avg(price)")->group("brand")->select();//分组查询

//$a = $nation->table("car")->field("Brand,avg(price)")->group("brand")->having("avg(price)>50")->select();//having条件查询

//$a = $nation->field("Info.code,Info.name as 'name',nation.name as '民族'")->join("Info on Nation.code=Info.Nation")->select();//用join连接的时候前面的field的列要加上别名,不然会出问题。

//$a = $nation->table("car")->distinct(true)->field("brand")->select();//distinct去重

//$a = $nation->where("code='n003'")->getField("name");//只能写列名,获取某一列的值。

//$a = $nation->count();//也可以放在连贯操作的最后用,求出数据量。

//$a = $nation->table("car")->max("price");

//使用原生的查询方法
//$sql = "select * from nation";
//$a = $nation->query($sql);//查询语句还是按照原来的调用方法。

//$sql = "update nation set name='矮人族' where code='n001'";
//$a = $nation->execute($sql);//增、删、改语句用execute调用。

//var_dump($a);
}
}


添加

<?php
namespace Admin\Controller;
use Think\Controller;
class MainController extends Controller
{
public function showList()
{
echo "大苹果商城";
}

public function test()
{
$nation = D("Nation");//连接数据库中的Nation表

//数据添加
//1.使用数组添加,数组必须为关联数组,索引为列名。
//$attr = array("Code"=>"n090","Name"=>"虫族");
//$nation->add($attr);

//2.AR方式实现数据添加
//$nation->Code = "n030";
//$nation->Name = "朝鲜族";
//$nation->add();
//$this->redirect('New/category', array('cate_id' => 2), 5, '页面跳转中...');//上面的用法是停留5秒后跳转到New模块的category操作,并且显示页面跳转中字样,重定向后会改变当前的URL地址。

}

public function zhuCe()
{
//3.自动收集表单(非常好用的一点)
//要实现2个逻辑
//(1)显示注册页面
//(2)向数据库添加内容
if(empty($_POST))
{
$this->show();
}
else
{
//自动收集表单
$n = D("Nation");
$n->create();//自动收集表单的写法。前提是必须有post数组才能用create方法收集到。表单的name必须是数据库的列名。

//$n->sex = "";//需要处理的列单独拿出来处理,比如nation表中男女存的是布尔型数据1和0,而用户在填写表单的时候输入的是男或女,在这里单独修改一下,男=1,女=0.

$z = $n->add();
if($z)//success和error方法的第一个参数表示提示信息,第二个参数表示跳转地址,第三个参数是跳转时间(单位为秒)。
{
$this->success("添加成功","zhuCe");//如果成功先输出这一句话,再跳转到zhuCe方法。
}
else
{
$this->error("添加失败");//错误页面的默认跳转页面是返回前一页,通常不需要设置
}
}

}

public function canShu($b=0)//注意不能使用a、c、m、s传值,这里给变量一个默认值,防止报错。
{
//$id = $_GET["id"];
//echo "$id";
echo $b;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: