【问题标题】:Can't echo out rows from database Class using PDO, get 'ArgumentCountError' [duplicate]无法使用 PDO 从数据库类中回显行,获取“ArgumentCountError”[重复]
【发布时间】:2019-02-28 14:38:28
【问题描述】:

我正在尝试使用 PDO 通过我的检索类从我的数据库中回显数据。我很难做到这一点。有人可以帮帮我吗?

这个错误显示:

致命错误:未捕获的 ArgumentCountError:函数 Retrieve::__construct() 的参数太少,第 89 行的 index.php 中传递了 0,而 index.php:58 中预期的正是 1 堆栈跟踪:#0 index.php(89 ): Retrieve->__construct() #1 {main} 在第 58 行的 index.php 中抛出

这是我的代码:

<?php 
class Database {
	private $host = 'localhost';
	private $db_name = 'photos';
	private $username = 'root';
	private $password = '';
	private $conn;

	public function connect() {	

		try {
			$this->conn = new PDO('mysql:host=' . $this->host . ';dbname=' . $this->db_name, $this->username, $this->password);
			$this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
			echo "Connected successfully"; 
		} catch(PDOException $e) {
			echo 'Connection Error: ' . $e->getMessage();
		}

		$this->conn = null;
	}

}

class Retrieve extends Database {
	
	private $conn;
	private $table = 'indeximg';

	public $id;
	public $username;
	public $img;

	public function __construct($db) {
		$this->conn = $db;
	}

	public function read() {
		$query = 'SELECT id, username, img FROM' . $this->table;
		$stmt = $this->conn->prepare($query);
		$stmt->execute();
		return $stmt;
	}

}


$object = new Retrieve;
echo $object->read();
?>

【问题讨论】:

    标签: php sql database oop pdo


    【解决方案1】:

    参数丢失。

    $object = new Retrieve;
    //Should be $object = new Retrieve($your_db);
    

    您已要求对

    public function __construct($db) {
            $this->conn = $db;
        }
    

    因为你已经从父母那里继承了

    为什么不只是 在你的 __construct 中添加 $this-&gt;connect() 而不是

    $db = new Database();
    $db->connect();
    $object = new Retrieve($d);
    echo $object->read();
    

    【讨论】:

    • 感谢您解决此问题,但现在我遇到了另一个错误。它是:连接成功致命错误:未捕获错误:调用 index.php:39 中的未定义方法 Database::prepare() 堆栈跟踪:#0 index2.php(50): Retrieve->read() #1 {main}在第 39 行的 index.php 中抛出
    • @codo7081 我已经更新了我的答案,
    • 我也试过了,但我仍然遇到错误。感谢您的帮助,我会找出解决方法。
    • @codo7081 可以告诉我你的课程检索吗?
    【解决方案2】:

    根据您的代码,Retrieve 的构造函数需要一个参数:$db,而您在此处创建实例时没有传递任何参数:

    $object = new Retrieve; // <-- where is the argument?
    echo $object->read();
    

    此时您的代码令人困惑。如果 Retrieve 类在构造函数中需要一个 Database 的实例,为什么它要扩展 Database 类?

    试试这个:

    <?php 
    class Database {
        private $host = 'localhost';
        private $db_name = 'photos';
        private $username = 'root';
        private $password = '';
        private $conn;
    
        public function connect() { 
    
            try {
                $this->conn = new PDO('mysql:host=' . $this->host . ';dbname=' . $this->db_name, $this->username, $this->password);
                $this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
                echo "Connected successfully"; 
            } catch(PDOException $e) {
                echo 'Connection Error: ' . $e->getMessage();
            }
    
            $this->conn = null;
        }
    
    }
    
    class Retrieve {
    
        private $conn;
        private $table = 'indeximg';
    
        public $id;
        public $username;
        public $img;
    
        public function __construct($db) {
            $this->conn = $db;
        }
    
        public function read() {
            $query = 'SELECT id, username, img FROM' . $this->table;
            $stmt = $this->conn->prepare($query);
            $stmt->execute();
            return $stmt;
        }
    
    }
    
    
    $db = new Database();
    $db->connect();
    $object = new Retrieve($db);
    echo $object->read();
    ?>
    

    我删除了 Retrieve 的继承并将正确的构造函数参数传递给 Retrieve。

    【讨论】:

    • 我从教程中借用了一些代码,我只是在自己的代码上添加。您能够修复我遇到的第一个错误(非常感谢),但现在我遇到了另一个错误。
    • 我得到的新错误是:
    • 连接成功致命错误:未捕获错误:调用未定义方法 Database::prepare() in index.php:39 堆栈跟踪:#0 index2.php(50): Retrieve->read() #1 {main} 在第 39 行的 index.php 中抛出我不知道如何解决这个问题
    • 如果您查看 Database 类,那里没有定义“prepare”方法,只有“connect”。您似乎没有从教程中完全导入该类。
    • 这对我来说仍然太混乱了。我会尽力解决这个问题,谢谢你的建议,很有帮助
    猜你喜欢
    • 1970-01-01
    • 2022-11-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-12
    • 2012-10-22
    • 1970-01-01
    相关资源
    最近更新 更多