批改状态:合格
老师批语:
class Query(){protected $db;//PDO连接对象protected $table;protected $field;protected $where;protected $Limit;protected $select;protected $toArray;//连接数据库private function connect($dsn,$username,$password){$this->db=new PDO($dsn,$username,$password);}function __construct($dsn,$username,$password){$this->connect($dsn,$username,$password);}//数据表方法$tablepublic function table($table){$this->table=$table;return $this;}//文件名方法$fieldpublic function field($field){$this->field=$field;return $this;}//Limit方法$Limitpublic function Limit($Limit){$this->Limit=$Limit;return $this;}//$where方法public function where($where){$this->where=$where;return $this;}//$select方法public function select($select){$this->select=$select;return $this;}public function getSql(){return printf('SELECT %s FROM $s LIMIT %d',$this->table,$this->field,$this->limit);}//执行查询public function select(){return $this->db->query($this->getSql())->fetchAll(PDO::FETCH_ASSOC);}}//工作类class Db{static function __callStatic($method,$args){$dsn='mysql:host=localhost;dbname=news';$username='root';$password='123456tyi';$query=new Query( $dsn, $username, $password);//直接委托给query中的具体方法来完成return call_user_func([$query,$method],...args);}}$res=Db::table('userid')->field('uid,uname')->Limit(1)->select();echo '<pre>';print_r($res);
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号