标签:
小李子:用于演示作用的数据库表:customers
官方解读: “ 读取数据集其实就是获取数据表中的多行记录(以及关联数据),使用select
方法 ”
1 $customers=D(‘customers‘); 2 $customers_msg_all=$customers->select(); 3 print_r($customers_msg_all);die;
最简单无脑的select查出来的数据如下:
1 Array 2 ( 3 [0] => Array 4 ( 5 [id] => 21 6 [brand_id] => 15 7 [name] => anme 8 [iccid] => 1882*** 9 [mobile] => 18826** 10 [addtime] => 11 [pretime] => 12 [taocan] => 13 [huafei] => 14 [liuliang] => 15 [beizhu] => 16 [huzhaoid] => 3660 17 [rutaiid] => 3660 18 [tongxingid] => 3660 19 [status] => 0 20 [des] => 21 [tongxingimg] => 2016-08-15/57b19aaeb008c.png 22 [rutaiimg] => 2016-08-15/57b19aaeaf58e.png 23 ) 24 25 [1] => Array 26 ( 27 [id] => 22 28 [brand_id] => 16 29 [name] =>ads 30 [iccid] => 132**21 31 [mobile] => 138***1 32 [addtime] => 33 [pretime] => 34 [taocan] => 35 [huafei] => 36 [liuliang] => 37 [beizhu] => 38 [huzhaoid] => 123456 39 [rutaiid] => 123456 40 [tongxingid] => 123456 41 [status] => 0 42 [des] => 43 [tongxingimg] => 2016-08-15/57b19acc5b4a5.png 44 [rutaiimg] => 2016-08-15/57b19acc5a9e6.png 45 ) 46 47 [2] => Array 48 ( 49 [id] => 25 50 [brand_id] => 17 51 [name] => kils 52 [iccid] => 1890** 53 [mobile] => 189*** 54 [addtime] => 55 [pretime] => 56 [taocan] => 57 [huafei] => 58 [liuliang] => 59 [beizhu] => 60 [huzhaoid] => 123123 61 [rutaiid] => 3213123 62 [tongxingid] => 321312 63 [status] => 0 64 [des] => 65 [tongxingimg] => 2016-08-15/57b19bef2b9db.png 66 [rutaiimg] => 2016-08-15/57b19bef2ae22.png 67 ) 68 69 )
官方解读:“ 读取数据是指读取数据表中的一行数据(或者关联数据),主要通过find
方法完成 ”:
现在看看find查出来的数据:
1 $customers=D(‘customers‘); 2 $customers_msg_all=$customers->find(); 3 print_r($customers_msg_all);die;
1 Array 2 ( 3 [id] => 21 4 [brand_id] => 15 5 [name] => jiangjun 6 [iccid] => 1882**0 7 [mobile] => 1882***0 8 [addtime] => 9 [pretime] => 10 [taocan] => 11 [huafei] => 12 [liuliang] => 13 [beizhu] => 14 [huzhaoid] => 3660 15 [rutaiid] => 3660 16 [tongxingid] => 3660 17 [status] => 0 18 [des] => 19 [tongxingimg] => 2016-08-15/57b19aaeb008c.png 20 [rutaiimg] => 2016-08-15/57b19aaeaf58e.png 21 )
看到没,find只查出了数据表之中一组数据,即使你不给where条件;
现在来看用的最多,用法最多变的getField方法;
官方解读:“ 读取字段值其实就是获取数据表中的某个列的多个或者单个数据,最常用的方法是 getField
方法 ” 注意,getField里面必须传递需要查询的数据表字段名称,否则就是,查询出来的数据是空的,而不是全部数据
1 $customers=D(‘customers‘); 2 $customers_msg_all=$customers->where(‘id=21‘)->getField(‘mobile‘); 3 print_r($customers_msg_all);die;
1 18826*****
输出一个varchar数据,并非数组,可以直接用;
用getField输出数据表某一字段某一列的值:
$customers=D(‘customers‘); $customers_msg_all=$customers->getField(‘mobile‘); print_r($customers_msg_all);die;
当你这样写的时候,仍然是仅仅输出一个电话号码,也就是数据库字段mobile的某一个数值(最后一个);
1 $customers=D(‘customers‘); 2 $customers_msg_all=$customers->getField(‘mobile‘,true); 3 print_r($customers_msg_all);die;
输出结果:
1 Array 2 ( 3 [0] => 1323**21 4 [1] => 188**60 5 [2] => 1890***198 6 )
所以,雄哥说的加true是为了返回一维数组,之意是为了获取某一列的全部数值;
1 $customers=D(‘customers‘); 2 $customers_msg_all=$customers->getField(‘mobile‘,2); 3 print_r($customers_msg_all);die;
将true改为2,意思是取出该列的数值,限制取出2条数据:
1 Array 2 ( 3 [0] => 1323**8 4 [1] => 1882***60 5 )
getField里面传入多个字段名称呢???
1 $customers=D(‘customers‘); 2 $customers_msg_all=$customers->getField(‘mobile,name‘);
3 print_r($customers_msg_all);die;
输出:
1 Array 2 ( 3 [18826**60] => name1 4 [1323***9621] => name2 5 [189***9198] => name3 6 )
其他传入3个或者以上数据库字段的,功能与select有所重叠,有比较少使用,不做演示;
总的来说啊;查询整个表的数据用select;
查询表的某一行数据用find;
查询表的某一列数据或者某一个数据用getField;
ThinkPHP之中的getField、Find、select、返回数据类型详解(ThinkPHP之中所有数据读取了)
标签:
原文地址:http://www.cnblogs.com/haveadream435/p/5792910.html