0

I'm trying to echo out data from my database through my Retrieve Class using PDO. I'm having trouble doing that. Can someone please help me?

This error shows:

Fatal error: Uncaught ArgumentCountError: Too few arguments to function Retrieve::__construct(), 0 passed in index.php on line 89 and exactly 1 expected in index.php:58 Stack trace: #0 index.php(89): Retrieve->__construct() #1 {main} thrown in index.php on line 58

This is my code:

<?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();
?>
codo7081
  • 25
  • 7

2 Answers2

1

From your code, the constructor for Retrieve expects one argument: $db, and you didn't pass any when you created an instance here:

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

Your code is confusing at this point. If the Retrieve class expects an instance of Database in the constructor, why does it extend the Database class?

Try this:

<?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();
?>

I removed the inheritance of Retrieve and passed the proper constructor argument to Retrieve.

Raisen
  • 4,385
  • 2
  • 25
  • 40
  • I borrowed some of this code from a tutorial, I just added on my own code. You were able to fix the first error I got (thank you very much), but now I'm getting another error. – codo7081 Sep 25 '18 at 04:25
  • The new error I'm getting is: – codo7081 Sep 25 '18 at 04:25
  • Connected successfully Fatal error: Uncaught Error: Call to undefined method Database::prepare() in index.php:39 Stack trace: #0 index2.php(50): Retrieve->read() #1 {main} thrown in index.php on line 39 I'm not sure how to fix this – codo7081 Sep 25 '18 at 04:27
  • If you look at the Database class, there is no "prepare" method defined there, only "connect". It looks like you didn't fully import the class from the tutorial. – Raisen Sep 25 '18 at 04:34
  • This is still too confusing to me. I'll try to resolve this, thank you for your advice, is very helpful – codo7081 Sep 25 '18 at 04:48
0

Argument missing on.

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

You have requested an argument on

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

And since you already inherited from parent

Why don't just add $this->connect() in your __construct instead of

$db = new Database();
$db->connect();
$object = new Retrieve($d);
echo $object->read();
Deno
  • 434
  • 4
  • 16
  • Thanks for fixing the issue but now I'm getting another error. It is: Connected successfully Fatal error: Uncaught Error: Call to undefined method Database::prepare() in index.php:39 Stack trace: #0 index2.php(50): Retrieve->read() #1 {main} thrown in index.php on line 39 – codo7081 Sep 25 '18 at 04:28
  • @codo7081 I've updated my answer, – Deno Sep 25 '18 at 04:48
  • I've tried that too but I'm still getting errors. Thanks for your help I'll find out how to fix it. – codo7081 Sep 25 '18 at 05:13
  • @codo7081 can show me your class Retrieve ? – Deno Sep 25 '18 at 06:14