Tp中与数据库的相关语句

TP框架中给予了使用者一些更简单的与数据库互动的方法。这里我就用Mysqli为例子做个演示。

namespace HomeController;
use ThinkController;
class MainController extends Controller {
    public function index()
    {    
        //$n = new HomeModelInfoModel();
        //$n = D("info");//创建模型对象
        //$n = M("info");//创建模型对象,是父类对象
        //var_dump($n);
        
        //$n = M("info");
        //得到数据的方法
        //$arr = $n->select();//读取所有数据
        //$arr = $n->select("p001,p002");//此处只能查主键值
        //$arr = $n->find("p001");//读一条数据
        //$arr = $n->count();//返回该表的数据总数
        $n = M("car");
        //$arr = $n->max("price");
        //$arr = $n->sum("price");//求和  avg平均值
        //连贯操作
        //$arr = $n->where("price>60")->select();//where加查询条件
        //$arr = $n->table("info")->select();//切换数据表
        //$arr = $n->field("code,name")->select();//确定查找的列
        //$arr = $n->order("price desc")->select();//排序
        //$arr = $n->limit(4,4)->select();//给一个数是取前几条。两个数就是跳过多少查多少。用来做分页
        //$arr = $n->page(3,3)->select();//分页
        //$arr = $n->field("info.Code as icode,info.name as iname,sex,nation.name as nname,birthday")->join("nation on info.Nation=nation.Code")->select();//连接查询
        //$arr = $n ->field("brand")->distinct("brand")->select();//去重
        //TP框架也是支持原生的SQL语句的,但是会没有防错的功能。
        //$arr = $n->query("select * from info");
        //$arr = $n->execute("insert into nation values('n901','代号')");//执行其他语句
        var_dump($arr);
    }

这些都是用到的一些语句,因为太繁琐,我就不去一一演示了。

原文地址:https://www.cnblogs.com/nzhcww/p/7145916.html