dd

万能单条件查询函数

jerry thinkphp 2015年11月18日 收藏
万能单条件查询函数,如代码。
函数Getx2x有四个参数:
第一个参数[必须]为表名;
第二个参数[必须]为字段名;
第三个参数[必须]为所查询字段的值。
第四个参数[可选]为所查询的字段名,未填默认为'id'。

//例子1:
//要查询think_user表中,id=12的用户名:
$username=Getx2x('User','username','12');
//或
$username=Getx2x('User','username','12','id');
注:此时$u
函数代码请放于位于项目目录下Common/Common.php中
//万能单条件查询函数,由lisan制作
    function Getx2x($table,$fields,$id,$str){
        $aa=M($table);
        if(empty($str)){
            $expression='getByid';
        }else{
            $expression='getBy'.$str;
        }
        $thisaa=$aa->field($fields)->$expression($id);

        $bb=explode(',',$fields);
        if(count($bb)<=1){
            return $thisaa[$fields];
        }else{
            return $thisaa;
        }        
    }
dd