PHP PDO Class with multiple Select Efficiency and speed I am very new to classes, and not sure how efficient this class is, would appreciate improvement feedback and info on efficiency including speed.

The class includes a construct that creates a connection to the database and two other functions that get data from the database and return some values concated with some HTML.

This is made from code that I've found around the web mostly SO, so would be good if suggestions were easy to understand for a novice.

Can improvements be made to this so that it is efficient and best practices are incorporated?

&lt;?php
class MyClass {
protected $PDO;

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

public function getCount($sq,$bkid,$bkname) {
    $sth = $this-&gt;PDO-&gt;prepare("SELECT COUNT(sentence) AS b FROM sentence, chapters, books WHERE MATCH(sentence) AGAINST (:q IN BOOLEAN MODE) AND chapters.book_Id = books.book_Id AND sentence.chapter_Id = chapters.chapter_Id AND books.book_Id = :bk");
    $sth-&gt;execute(array(':q' =&gt; $sq, ':bk' =&gt; $bkid));
    $row = $sth-&gt;fetch(PDO::FETCH_ASSOC);
    $total = $row['b'];
    if($total!=0)
    {
    $ensq = urlencode($sq);
    $count = "&lt;a href='filter.php?id=$bkid&amp;amp;q=$ensq'&gt;$bkname&lt;/a&gt;&lt;sup&gt;$total&lt;/sup&gt; ";
    return $count;
    }
    else
    {
    return;
    }
}

public function getwriter($sqn) {
    $nsth = $this-&gt;PDO-&gt;prepare("SELECT COUNT(DISTINCT(writer)) AS n FROM sentence WHERE MATCH(writer) AGAINST (:q IN BOOLEAN MODE)");
    $nsth-&gt;execute(array(':q' =&gt; $sqn));
    $nrow = $nsth-&gt;fetch(PDO::FETCH_ASSOC);
    $ntotal = $nrow['n'];
    if($ntotal!=0)
    {
    $nensq = urlencode($sqn);
    $ncount = "&lt;a href='nsearch.php?q=$nensq'&gt;writers&lt;/a&gt;&lt;sup&gt;$ntotal&lt;/sup&gt; ";
    return $ncount;
    }
    else
    {
    return;
    }
}

public function getChapter($cqn) {
    $csth = $this-&gt;PDO-&gt;prepare("SELECT COUNT(chapter_Title) AS c FROM chapters WHERE MATCH (chapter_Title) AGAINST (:q IN BOOLEAN MODE)");
    $csth-&gt;execute(array(':q' =&gt; $cqn));
    $crow = $csth-&gt;fetch(PDO::FETCH_ASSOC);
    $ctotal = $crow['c'];
    if($ctotal!=0)
    {
    $censq = urlencode($cqn);
    $ccount = "&lt;a href='csearch.php?q=$censq'&gt;Chapters&lt;/a&gt;&lt;sup&gt;$ctotal&lt;/sup&gt; ";
    return $ccount;
    }
    else
    {
    return;
    }
}
}
$dbh = new PDO('mysql:host=localhost;dbname=dbname;charset=utf8', 'db', 'pass');
?&gt;


And also is it ok to call the function multiple times next to each other like so:

$t = new MyClass($dbh);
echo $t-&gt;getCount($search,11,"Book 1");
echo $t-&gt;getCount($search,4,"Book 2");
echo $t-&gt;getCount($search,39,"Book 3");
echo $t-&gt;getCount($search,56,"Book 4");
echo $t-&gt;getWriter($search);
echo $t-&gt;getChapter($search);
$dbh = null;
The most important thing you have to learn about classes: every method should be responsible for one task only. Means a method that gets you a count should return you a count, not some HTML.

Two other minor improvements would be a better SQL formatting, so anyone could actually read it and a nice helper function that exists in PDO, fetchColumn():

So your methods should be like

public function getCount($sq,$bkid) {
    $sql = "SELECT COUNT(sentence)
            FROM sentence, chapters, books 
            WHERE MATCH(sentence) AGAINST (:q IN BOOLEAN MODE) 
                AND chapters.book_Id = books.book_Id 
                AND sentence.chapter_Id = chapters.chapter_Id 
                AND books.book_Id = :bk"
    $sth = $this-&gt;PDO-&gt;prepare($sql);
    $sth-&gt;execute(array(':q' =&gt; $sq, ':bk' =&gt; $bkid));
    return $sth-&gt;fetchColumn();
}


Whereas for the HTML formatting there should be another method/function. Let me suggest to you something like this:

function getLink($title, $count, $parameters)
{
    if ($count)
    {
        $qs = http_build_query($parameters);
        $title = htmlspecialchars($title);
        return "&lt;a href='filter.php?$qs'&gt;$title&lt;/a&gt;&lt;sup&gt;$total&lt;/sup&gt;\n";
    }
}


it could be used like this:

$count = $t-&gt;getCount($search, 11);
echo getLink("Book 1", $count, ['id' =&gt; 11, 'q'=&gt; $search]);


Here, as you can see, a handy http_build_query() would take care of encoding your keys and values.