类内的PHP pdo
试图在另一个类中执行pdo语句,但出现错误: PHP可捕获致命错误:传递给产品的参数1::_construct()必须是PDO的实例,未给定,在中调用 如果我从构造函数中删除PDO,我会得到:类内的PHP pdo,php,pdo,Php,Pdo,试图在另一个类中执行pdo语句,但出现错误: PHP可捕获致命错误:传递给产品的参数1::_construct()必须是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 __construc
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。Hm。。。这可能也是一个名称空间问题。尝试
public function\u构造(\PDO$dbh)
和$dbh=new\PDO(“…
我可能是错的,但闻起来有点像。可能是我尝试过的重复,但仍然得到PHP可捕获的致命错误:参数1传递给产品::\u构造()必须是PDO的一个实例,未给定,从第行调用
,其中为_u-constructHm…这里可能也存在名称空间问题。请尝试public function _; construct(\PDO$dbh)和$dbh=new\PDO(“..我可能是错的,但闻起来确实有点像。可能是我用\尝试的重复,但我仍然得到了PHP可捕获的致命错误:传递给产品的参数1::\uuuu construct()必须是PDO的实例,没有给定,从where是\uu construct的行调用