# Creating a database class in PHP with MySQLi

I am creating a database class in PHP but I feel that there's something wrong with my code. Is there any suggestion to refactor this? I feel like there's something wrong and missing in this code.

<?php
require_once("db_constants.inc.php");

Class MySqli_Database
{
public $db; public function __construct($db_host,$db_username,$db_password,$db_name) {$this->db = new mysqli($db_host,$db_username,$db_password,$db_name);
}

public function __performSql($sql) { } public static function closeConnection($conn)
{
$this->db_close(); } }$conn = new MySqli_Database (DB_HOST, DB_USERNAME, DB_PASSWORD, DB_NAME)
or die("Could Not Connect To Database: " . mysqli_errno());


Perform SQL will just basically execute the query, though I haven't included the code yet, but I will soon. Also if you have tutorials or guide when creating a database class for MySQLi please post it. It would be a great help.

• What does this class offer that wasn’t available before? At the moment, nothing. – Konrad Rudolph Jun 26 '12 at 9:30

What is the purpose of this class?

Creating a database class in PHP with mySQLI

MySQLi is a "database class." (Whatever meaning of "database class" you choose)

• Are you trying to make a class that adds on to MySQLi?
• Are you trying to make a class that abstracts away certain operations (a DBAL)?
• Are you trying to make a class that maps objects to a database (ORM)?

require_once("db_constants.inc.php");

The file that your class is defined in does not need to be aware of your connnection details. Only the file that uses the class needs to know that, not the file that defines the class (unless of course, it's the same file, but it rarely should be).

or die

Constructors cannot return values, so this will never work.

The proper way to handle errors with objects is exceptions. Please note though that exceptions should only be used when something that's actually exceptional happens. It's far to easy to fall into the anti-pattern of using exceptions as return values.

In the case of mysqli's constructor failign to create a connection, that is definitely a cause for an exception. (I tend to avoid the possibility of an object being in an unstable state.)

closeConnection

Why does this take a parameter when the connection is encapsulated in the class?

Also, unless you're going to have an openConnection method, avoid this. Just let the destructor handle this. Why would a user ever want to close the connection but keep the instance alive?

PDO vs MySQLi

I'm heavily biased in this one since I've always used PDO and never written a single line of code with MySQLi, but I've never seen the advantage of MySQLi over PDO. I suppose there's a few MySQL functionalities that MySQLi supports and PDO doesn't, but they must be quite edge cases as I've never run into anything.

Concluding Remarks

I feel like there's barely anything here to review. The goal of your code is not quite clear. Can you explain a little more what your end goal is? What is the purpose of this class? What is an example use case?

• Sorry, I am really not familiar with the terms, but I aim to to make a class that abstracts away certain operations and also Are you trying to make a class that maps objects to a database. That's it. – user962206 Jun 26 '12 at 14:20
• @user962206 What you're describing are called a DBAL and an ORM, respectively. They are distinct ideas and (typically) libraries. They're also extremely difficult to get right (though also extremely interesting to design). You may want to look into a few projects like Doctrine DBAL, Doctrine ORM and Propel for ideas. – Corbin Jun 27 '12 at 9:09