More importantly than this specific class not working is the design you have implemented here is non-orthogonal. Good code should follow the DRY (don't repeat yourself) principle. In this class, the intent is to manage content. However, you are dealing with a bunch of database connection stuff. This means the class is doing two things: database connection management, as well as content management. This is non-orthogonal code.
To fix this, you should define a requirement for the construction of your ContentManager class. Namely, that it be passed a valid, connected database handle:
public function __construct(mysqli $connection){
$this->db = $connection;
}
So the db handle needs to come from somewhere. This should be a DB connection factory method.
class DbFactory {
public static function getConnection($host, $username, $password, $dbname) {
//alternatively load this information from a configuration file for your site,
//perhaps using a standard format/utility pair like json
return new mysqli($host, $username, $password, $dbname);
}
}
Now you can create a unit test to ensure that you are able to generate a valid connection from your db factory (and test it in the unit test). And a separate test to ensure that when given a valid db handle your connection manager will call the correct methods on it..
Since you have no other specific indicating an error message I can't outline how to create a test case to reproduce and then fix that error, but that is the next step.
Finally, you could take it one step further and instead of writing raw sql for every insert and select statement, just use the model/mapper pattern. This pattern uses the field names of your data class to generate the sql statements for corresponding inserts and updates. This frees you from having to write lots and lots of potential sql for common simple things like 1:1 mapping of data in your php app to data in tables in your database.
Code I write using this pattern ends up looking something like this:
class Content {
protected $id;
protected $pageName;
protected $pageContent;
}
class ContentMapper{
protected $tableName = 'content';
public function __construct($conn) {
$this->conn = $conn;
}
public function find($id) {
$result = $this->conn->
select()->
from($this->tableName)->
where('id', $id);
if(isset($result[0])) {
$returnContent = new Content();
$returnContent->id = $result[0]['id'];
$returnContent->pageName = $result[0]['pageName'];
$returnContent->pageContent = $result[0]['pageContent'];
return $returnContent;
}
return null;
}
public function save(Content $content) {
$result = $this->conn->
insertOrUpdate()->
field('id', $content->id)
field('pageName', $content->pageName)
field('pageContent', $content->pageContent)
where('id', $content->id);
}
}
This is kind of rough code, but you get the general picture. You can pm me if you want full examples of how this works in practice, but it is very simple and clean.