ThinkPHP之中的getField、Find、select、返回数据类型详解(ThinkPHP之中所有数据读取了)

时间:2024-04-16 14:05:22

小李子:用于演示作用的数据库表:customers

官方解读: “ 读取数据集其实就是获取数据表中的多行记录(以及关联数据),使用select方法 ”

         $customers=D('customers');
         $customers_msg_all=$customers->select();
         print_r($customers_msg_all);die;    

最简单无脑的select查出来的数据如下:

 Array
 (
     [0] => Array
         (
             [id] => 21
             [brand_id] => 15
             [name] => anme
             [iccid] => 1882***
             [mobile] => 18826**
             [addtime] =>
             [pretime] =>
             [taocan] =>
             [huafei] =>
             [liuliang] =>
             [beizhu] =>
             [huzhaoid] => 3660
             [rutaiid] => 3660
             [tongxingid] => 3660
             [status] => 0
             [des] =>
             [tongxingimg] => 2016-08-15/57b19aaeb008c.png
             [rutaiimg] => 2016-08-15/57b19aaeaf58e.png
         )

     [1] => Array
         (
             [id] => 22
             [brand_id] => 16
             [name] =>ads
             [iccid] => 132**21
             [mobile] => 138***1
             [addtime] =>
             [pretime] =>
             [taocan] =>
             [huafei] =>
             [liuliang] =>
             [beizhu] =>
             [huzhaoid] => 123456
             [rutaiid] => 123456
             [tongxingid] => 123456
             [status] => 0
             [des] =>
             [tongxingimg] => 2016-08-15/57b19acc5b4a5.png
             [rutaiimg] => 2016-08-15/57b19acc5a9e6.png
         )

     [2] => Array
         (
             [id] => 25
             [brand_id] => 17
             [name] => kils
             [iccid] => 1890**
             [mobile] => 189***
             [addtime] =>
             [pretime] =>
             [taocan] =>
             [huafei] =>
             [liuliang] =>
             [beizhu] =>
             [huzhaoid] => 123123
             [rutaiid] => 3213123
             [tongxingid] => 321312
             [status] => 0
             [des] =>
             [tongxingimg] => 2016-08-15/57b19bef2b9db.png
             [rutaiimg] => 2016-08-15/57b19bef2ae22.png
         )

 )

官方解读:“ 读取数据是指读取数据表中的一行数据(或者关联数据),主要通过find方法完成 ”:

现在看看find查出来的数据:

 $customers=D('customers');
 $customers_msg_all=$customers->find();
 print_r($customers_msg_all);die;
 Array
 (
     [id] => 21
     [brand_id] => 15
     [name] => jiangjun
     [iccid] => 1882**0
     [mobile] => 1882***0
     [addtime] =>
     [pretime] =>
     [taocan] =>
     [huafei] =>
     [liuliang] =>
     [beizhu] =>
     [huzhaoid] => 3660
     [rutaiid] => 3660
     [tongxingid] => 3660
     [status] => 0
     [des] =>
     [tongxingimg] => 2016-08-15/57b19aaeb008c.png
     [rutaiimg] => 2016-08-15/57b19aaeaf58e.png
 )

看到没,find只查出了数据表之中一组数据,即使你不给where条件;

现在来看用的最多,用法最多变的getField方法;

官方解读:“ 读取字段值其实就是获取数据表中的某个列的多个或者单个数据,最常用的方法是 getField 方法 ” 注意,getField里面必须传递需要查询的数据表字段名称,否则就是,查询出来的数据是空的,而不是全部数据

 $customers=D('customers');
 $customers_msg_all=$customers->where('id=21')->getField('mobile');
 print_r($customers_msg_all);die;
 18826*****

输出一个varchar数据,并非数组,可以直接用;

用getField输出数据表某一字段某一列的值:

        $customers=D('customers');
        $customers_msg_all=$customers->getField('mobile');
        print_r($customers_msg_all);die;

当你这样写的时候,仍然是仅仅输出一个电话号码,也就是数据库字段mobile的某一个数值(最后一个);

 $customers=D('customers');
 $customers_msg_all=$customers->getField('mobile',true);
  print_r($customers_msg_all);die;

输出结果:

 Array
 (
     [0] => 1323**21
     [1] => 188**60
     [2] => 1890***198
 )

所以,雄哥说的加true是为了返回一维数组,之意是为了获取某一列的全部数值;

 $customers=D('customers');
 $customers_msg_all=$customers->getField('mobile',2);
 print_r($customers_msg_all);die;

将true改为2,意思是取出该列的数值,限制取出2条数据:

 Array
 (
     [0] => 1323**8
     [1] => 1882***60
 )

getField里面传入多个字段名称呢???

 $customers=D('customers');
 $customers_msg_all=$customers->getField('mobile,name'); 3 print_r($customers_msg_all);die;

输出:

 Array
 (
     [18826**60] => name1
     [1323***9621] => name2
     [189***9198] => name3
 )

其他传入3个或者以上数据库字段的,功能与select有所重叠,有比较少使用,不做演示;

总的来说啊;查询整个表的数据用select;

      查询表的某一行数据用find;

      查询表的某一列数据或者某一个数据用getField;

<form action="{:U('')}" method="get">这个小技巧呢,可以将地址栏的GET数据去除掉;

也就是加上U方法,而且空代表的是当前地址。