PDO __constructs期望至少有1个参数,给定0 [英] PDO __constructs expects at least 1 parameter, 0 given

查看:83
本文介绍了PDO __constructs期望至少有1个参数,给定0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有数据库类和构造函数:

I have database class and constructor function this:

<?php
class Connection {
private $PDO;

function __construct() {
    $username = 'root';
    $password = 'password';

    $PDO = new PDO('mysql:dbname=PROOV;host=localhost', $username, $password);

    return $this->PDO;
}
}
?>

以及扩展它的另一个类:

And the other class that extends it:

<?php

//$query = 'SELECT part_description FROM SparePartRequests LIMIT 100';

include_once 'connection.php';

class Proov extends PDO {

    public function returnRows() {
        $sth = $this->prepare('SELECT part_description FROM SparePartRequests LIMIT 100');
        $sth->execute();

        $result = $sth->fetch();

        return $result;
    }

}
    $proov = new Proov(); // <- this is line nr 19...

?>

它引发异常: 警告:PDO :: __ construct()需要至少1个参数,第19行的/var/www/proov/proov1.php中提供的参数为0

And it throws exception: Warning: PDO::__construct() expects at least 1 parameter, 0 given in /var/www/proov/proov1.php on line 19

我该如何解决我的问题? 感谢您的帮助!

How can i fix my problem? Thanks for any help!

感谢您的帮助!

推荐答案

但是您要扩展PDO-而不是Connection(并且连接保留了PDO对象-也不扩展它).您需要确定要使用哪种方法.

But you're extending PDO - not Connection (and connection keeps a PDO object - it does not extend it either). You need to decide which of these methods you want to use.

也许这就是您想要的吗?

Perhaps this is what you want?

class Connection extends PDO {
    public function __construct() {
        $username = 'root';
        $password = 'password';

        parent::__construct('mysql:dbname=PROOV;host=localhost', $username, $password);
    }
}

class Proov extends Connection { //We extend Connection - not PDO
    public function returnRows() {
        $sth = $this->prepare('SELECT part_description FROM SparePartRequests LIMIT 100');
        $sth->execute();

        $result = $sth->fetch();

        return $result;
    }
}

这篇关于PDO __constructs期望至少有1个参数,给定0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆