在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
思路:链式语法就是对象调用方法执行赋值操作后返回一个对象,那就可以接着调用方法,最后一个方法返回具体sql语句 <?php class Sql{ private $sql=array("from"=>"", "where"=>"", "order"=>"", "limit"=>""); public function from($tableName) { $this->sql["from"]="FROM ".$tableName; return $this; } public function where($_where='1=1') { $this->sql["where"]="WHERE ".$_where; return $this; } public function order($_order='id DESC') { $this->sql["order"]="ORDER BY ".$_order; return $this; } public function limit($_limit='30') { $this->sql["limit"]="LIMIT 0,".$_limit; return $this; } public function select($_select='*') { return "SELECT ".$_select." ".(implode(" ",$this->sql)); } } $sql =new Sql(); echo $sql->from("testTable")->where("id=1")->order("id DESC")->limit(10)->select(); //输出 SELECT * FROM testTable WHERE id=1 ORDER BY id DESC LIMIT 0,10 ?>
|
2022-07-18
2022-08-16
2022-11-06
2022-08-18
2022-08-15
请发表评论