Fetch and add data to MySql Here is my very first code which fetch data and add it to MySql. Please review my code and let me know if I'm on the right path. Is there anything I can improve? 

postsData class = fetch and add data blog posts

categoriesData class = fetch and add data for categories of my blog. Ex: bmw, nissan, opel, ford, etc

connect.php

class dbh{

        private $host = "localhost";
        private $user = "root";
        private $pwd = "";
        private $dbname = "test_blog";

        public $conn;                               

        public function __construct()
        {
            $this-&gt;conn = $this-&gt;connect();
        }


        private function connect() {
            $dsn = 'mysql:host=' . $this-&gt;host . ';dbname=' . $this-&gt;dbname;
            $pdo = new PDO ($dsn, $this-&gt;user, $this-&gt;pwd);
            $pdo-&gt;setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            $pdo-&gt;setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
            $pdo-&gt;setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
            return $pdo;

    }
}  


func.class.php

  class postsData extends dbh{

    public function getPosts() {

        $sql = "SELECT * FROM posts_tbl";
        $stmt = $this-&gt;conn-&gt;prepare($sql);
        $stmt-&gt;execute();
        $result = $stmt-&gt;fetchAll((PDO::FETCH_OBJ));

        return $result;

    }

    public function addPost($filter_author, $filter_title, $filter_txt) {

        $sql = "INSERT INTO posts_tbl (post_author, post_title, post_txt) VALUES (?, ?, ?)";
        $stmt = $this-&gt;conn-&gt;prepare($sql);
        $stmt-&gt;execute([$filter_author, $filter_title, $filter_txt]);

    }

}

class categoriesData extends dbh{


    public function getCategories () {

        $sql = "SELECT * FROM categories_tbl";
        $stmt = $this-&gt;conn-&gt;prepare($sql);
        $stmt-&gt;execute();
        $result = $stmt-&gt;fetchAll((PDO::FETCH_OBJ));

        return $result;

    }

    public function adCategory ($filter_name, $filter_slug) {

        $sql = "INSERT INTO categories_tbl (category_name, category_slug) VALUES (?, ?)";
        $stmt = $this-&gt;conn-&gt;prepare($sql);
        $stmt-&gt;execute([$filter_name, $filter_slug]);

    }

}

    //test
    $post = new PostsData();
    $posts = $post-&gt;getPosts();

    foreach ($posts as $post) { 
        echo $post-&gt;post_title . '&lt;br&gt;';
    }
Your code is mostly all right, especially given it's your first code attempt. 

I would say there is only on critical flaw. A post or a category is not a database connection. Hence it should be never extended from it. It would be like if you extend a human from a car simply because a human is going to ride a car. A car is a service used by a human, that needs to be injected into a human object. 

Besides, the way your code is written, it will create a new connection to the database in the  every class instance. Which will result in the "Too many connections"  error. 

To sum it up: 


a database connection has to be made only once. and then passed as a parameter to all your classes that need it. 
your dbh class is rather useless. it does't add anything to original PDO. so you can get rid of it and simply create a plain PHP file with connection code and include it in your scripts
then you have to rewrite your data classes adding a constructor, providing either vanilla PDO or your own database connection class' instance as a parameter


something like this

class categoriesData {
    protected $conn;
    public function __construct($conn) {
        $this-&gt;conn = $conn;
    }
    ...


and then use it like this

require_once 'pdo.php';
$post = new PostsData($pdo);
$posts = $post-&gt;getPosts();

foreach ($posts as $post) { 
    echo $post-&gt;post_title . '&lt;br&gt;';
}