【发布时间】: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();
?>
【问题讨论】: