Exercise of doing static method overloading to implement chained access to the database
demo5.php references the class of Query.php
Then access the method in Query, the page prompts me method' table' was not found in Database
The following is the source code, I hope it can help me check the error
The first one is Query.php
pdo = $pdo; } //调用表名 public function table($tableName) { $this->table = $tableName; //关键是这一步 return $this; } //调用字段 public function field($fields) { $this->field = $fields; //关键是这一步 return $this; } //设置查询条件 public function where($where) { $this->where = $where; return $this; } //设置显示数量 public function limit($limit) { $this->limit = $limit; return $this; } //创建SQL语句查询 public function select() { //设置查询条件 $fields = empty($this->field) ? '*' : $this->field; $where = empty($this->where) ? '' : ' WHERE ' . $this->where; $limit = empty($this->limit) ? '' : ' LIMIT ' . $this->limit; //SQL $sql = 'SELECT '.$fields. 'FROM' .$this->table. $where . $limit; //预处理执行 $stmt = $this->pdo->prepare($sql); $stmt->execute(); return $stmt->fetchAll(PDO::FETCH_ASSOC); } }
The second one is demo5 .php
field('cate_id, name, alias') ->where('cate_id>=2') ->select(); foreach($cats as $cat){ print_r($cat); }
Found the problem, it is line 65 of Query.php. When splicing, spaces should be added before and after FROM.
Page prompt method 'table' was not found in Database