Jump to content

improve my database class


SGUserFace

Recommended Posts

5 minutes ago, benanamen said:

You can start with using PDO, Dependency Injection, and removing internal system error output to the user and posting your code in the forum using the code tags.

Currently I only work with MYSQLI

I'm looking for more effective methods

Link to comment
Share on other sites

Unless you have some specific reason for using MySQLi, you really should switch to PDO.  Aside from the fact that PDO is the standard interface that everyone is expected to use these days, mysqli's API is pretty horrible in comparison to PDO.

Link to comment
Share on other sites

On 5/27/2018 at 9:28 AM, SGUserFace said:

Currently I only work with MYSQLI

I'm looking for more effective methods

using the PDO extension will eliminate about half of the code (the code related to dynamically binding inputs), which will significantly speed up the code's execution, and will eliminate the need for the get_result() method, making the code portable across php version, build, and configuration differences. the mysqli stmt ->get_result() method is php version specific and requires that php be built to use the mysqlnd driver and that driver must be present. i've even seen at least one thread where these conditions where met but a more direct socket connection was being used, rather than a tcp/ip connection, and the get_result() method was not available, breaking the code.

Link to comment
Share on other sites

@AdRock and anyone else, please don't follow the tutorial at that link. It is doing just about everything that a (database) class shouldn't do.

it has broken scope by using defined constants for the connection credentials, so that there can only be one connection to one type of database server in an application. the connection credentials should be supplied to the constructor at call time.

it needs to set the character set for the connection, turn off emulated prepared queries, and it should set the default fetch mode to assoc.

it should extend the base class, so that all of the properties and methods of the base class are directly available, without needing to write one line methods for each one.

it should not use php to check the type of variables and explicitly bind input data, as this limits the values to php's maximums, which for applications now a days can be less than your databases's definition and usage.

while you should use exceptions to handle errors, this class should not catch any connection exception, just store the error message, and continue running like nothing bad just happened. the application code should catch any exception it is responsible for handling and let php catch and handle the rest, where php will use its' error_reporting, display_errors, and log_errors settings to control what happens with the actual error information or where php will use any user defined exception handler.

 

 

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.