尝试在另一个类中执行pdo语句,但我收到错误:
PHP Catchable致命错误:传递给products :: __ construct()的参数1必须是PDO的一个实例,没有给出,在
中调用
如果我从构造函数中删除PDO,我得到:
PHP Fatal error: Call to a member function prepare() on a non-object in
units.class.php:
class units {
protected $dbh;
public function __construct(PDO $dbh)
{
$this->dbh = $dbh;
}
public function getOneById($unit_id)
{
$sth = $this->dbh->prepare("SELECT * FROM units WHERE unit_id = :id");
$sth->bindParam(':id', $unit_id, PDO::PARAM_INT);
$sth->execute();
$product = $sth->fetch();
$sth->closeCursor();
return $product;
}
pdo.php:
try
{
$dbh = new PDO("....
这两个文件都是index.php中的必需文件,并且在课程之前需要pdo.php。