做普通PHP项目转thinkphp时,字段自动完整匹配,ajax时前台数据一直取不到,后发现是select和find返回数据集有差异,参考下面方法修改。
$this->ajaxReturn($msg[0]); select返回的是二维数组,find返回一维数组。
thinkphp是比较好的php开发框架,能比较快速的开发MVC架构的管理系统,我们需要用到 select()和find()方法,两个方法都能返回数据集数组,但有什么不同呢?先看一下我的代码对比:
- $tech=M('techlevel','HR_CS_','DB_CONFIG2');
- $Data=$tech->where('id=1')->find();
- dump($Data);
- $Data=$tech->where('id=1')->select();
- dump($Data);
$tech=M('techlevel','HR_CS_','DB_CONFIG2'); $Data=$tech->where('id=1')->find(); dump($Data); $Data=$tech->where('id=1')->select(); dump($Data);
结果
- array(6) {
- ["ID"] => int(1)
- ["TechLevel"] => string(2) "10"
- ["Remark"] => string(4) "��"
- ["CreateDate"] => string(19) "2013-03-14 15:14:38"
- ["CreateBy"] => string(5) "admin"
- ["ROW_NUMBER"] => string(1) "1"
- }
- array(1) {
- [0] => array(6) {
- ["ID"] => int(1)
- ["TechLevel"] => string(2) "10"
- ["Remark"] => string(4) "��"
- ["CreateDate"] => string(19) "2013-03-14 15:14:38"
- ["CreateBy"] => string(5) "admin"
- ["ROW_NUMBER"] => string(1) "1"
- }
- }
array(6) { ["ID"] => int(1) ["TechLevel"] => string(2) "10" ["Remark"] => string(4) "��" ["CreateDate"] => string(19) "2013-03-14 15:14:38" ["CreateBy"] => string(5) "admin" ["ROW_NUMBER"] => string(1) "1" } array(1) { [0] => array(6) { ["ID"] => int(1) ["TechLevel"] => string(2) "10" ["Remark"] => string(4) "��" ["CreateDate"] => string(19) "2013-03-14 15:14:38" ["CreateBy"] => string(5) "admin" ["ROW_NUMBER"] => string(1) "1" } }
从上面的代码可以看出,find()返回一个一维数组,select()返回一个二维数组,所以在取值时有所不同,一维数组取值用$data["TechLevel"],二维数组取值用$data[0]["TechLevel"],由于一开始没了解这个用法,调试一天也取不值,最后有dump方法才看到两个方法的不同所在!