ThinkPHP万能单条件查询函数
万能单条件查询函数,如代码。 br / 函数Getx2x有四个参数: br / 第一个参数[必须]为表名; br / 第二个参数[必须]为字段名; br / 第三个参数[必须]为所查询字段的值。 br / 第四个参数[可
万能单条件查询函数,如代码。
函数Getx2x有四个参数:
第一个参数[必须]为表名;
第二个参数[必须]为字段名;
第三个参数[必须]为所查询字段的值。
第四个参数[可选]为所查询的字段名,未填默认为'id'。
//例子1:
//要查询think_user表中,id=12的用户名:
$username=Getx2x('User','username','12');
//或
$username=Getx2x('User','username','12','id');
注:此时$username为字符串。
//例子2:
//要查询think_user表中,id=12的用户名、昵称:
$username=Getx2x('User','username,nickname','12');
//或
$username=Getx2x('User','username,nickname','12','id');
注:此时$username为数组。
函数Getx2x有四个参数:
第一个参数[必须]为表名;
第二个参数[必须]为字段名;
第三个参数[必须]为所查询字段的值。
第四个参数[可选]为所查询的字段名,未填默认为'id'。
//例子1:
//要查询think_user表中,id=12的用户名:
$username=Getx2x('User','username','12');
//或
$username=Getx2x('User','username','12','id');
注:此时$username为字符串。
//例子2:
//要查询think_user表中,id=12的用户名、昵称:
$username=Getx2x('User','username,nickname','12');
//或
$username=Getx2x('User','username,nickname','12','id');
注:此时$username为数组。
//万能单条件查询函数,由lisan制作 //函数代码请放于位于项目目录下Common/Common.php中 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; } }
精彩图集
精彩文章