ThinkPHP

如何不报错

在Controller中的代码(注意$id = null)

$list = $PeopleModel->getPeople($id = null);

报错提示

[8] Undefined variable: con E:\xampp\htdocs\guanli\Application\Admin\Model\PeopleModel.class.php 第 18 行.

模型代码

//第18行代码
return $result = $m->where($con)->select();
<?php
namespace Admin\Model;
use Think\Model;
class PeopleModel extends Model{

    protected $_map = array(
        'id'        =>  'people_id',
        'name'      =>  'people_name',
        'age'       =>  'people_age',
        'idcard'    =>  'people_idcard',
    );

    public function getPeople($id){
        $m = M('people');
        if($id){
            $con['people_id'] = $id;
        }
        return $result = $m->where($con)->select();
    }

    public function deletePeople($id){
        $m = M('people');
        $con['people_id'] = $id;
        return $result = $m->where($con)->delete();
    }

}

要怎么写 才可以不报错?

猴哥哥们要说清楚哦 不然可能不会被采纳呢!

阅读 1.8k
1 个回答

参数$con要做初始化,不然你$id不传的时候就没有$con的定义了

public function getPeople($id){
    $m = M('people');
    $con = array();
    if($id){
        $con['people_id'] = $id;
    }
    return $result = $m->where($con)->select();
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
宣传栏