SGUserFace Posted May 27, 2018 Share Posted May 27, 2018 (edited) How can I improve my database class? https://pastebin.com/fz7NpZj7 Edited May 27, 2018 by SGUserFace Quote Link to comment https://forums.phpfreaks.com/topic/307310-improve-my-database-class/ Share on other sites More sharing options...
benanamen Posted May 27, 2018 Share Posted May 27, 2018 (edited) 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. Edited May 27, 2018 by benanamen Quote Link to comment https://forums.phpfreaks.com/topic/307310-improve-my-database-class/#findComment-1558602 Share on other sites More sharing options...
SGUserFace Posted May 27, 2018 Author Share Posted May 27, 2018 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 Quote Link to comment https://forums.phpfreaks.com/topic/307310-improve-my-database-class/#findComment-1558603 Share on other sites More sharing options...
kicken Posted May 27, 2018 Share Posted May 27, 2018 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. Quote Link to comment https://forums.phpfreaks.com/topic/307310-improve-my-database-class/#findComment-1558604 Share on other sites More sharing options...
mac_gyver Posted May 29, 2018 Share Posted May 29, 2018 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. Quote Link to comment https://forums.phpfreaks.com/topic/307310-improve-my-database-class/#findComment-1558633 Share on other sites More sharing options...
AdRock Posted May 29, 2018 Share Posted May 29, 2018 Have a look at this PDO class. https://www.culttt.com/2012/10/01/roll-your-own-pdo-php-class/ I started with MySQLi and within a few hours switched to PDO. People I work with like MySQLi and they can't see the benefit of PDO. Drives me nuts ? Quote Link to comment https://forums.phpfreaks.com/topic/307310-improve-my-database-class/#findComment-1558636 Share on other sites More sharing options...
mac_gyver Posted May 29, 2018 Share Posted May 29, 2018 @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. Quote Link to comment https://forums.phpfreaks.com/topic/307310-improve-my-database-class/#findComment-1558645 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.