做静态方法重载实现数据库的链式访问的练习
demo5.php引用了Query.php的类
然后访问Query中的方法,页面提示我方法 'table' 在 Database 中未找到
以下是源代码,希望可以帮我检查出错误
第一个是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);
}
}第二个是demo5.php
field('cate_id, name, alias')
->where('cate_id>=2')
->select();
foreach($cats as $cat){
print_r($cat);
} Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
找到问题了,是Query.php的第65行,做拼接的时候,FROM前后应该加上空格。
页面提示方法 'table' 在 Database 中未找到