Database Access Class

Since version 2.0, there is now a class for database access. This class is similar to the table access classes of 2 parts. One general class DB and the respective extension, in our case MySqlDb.

Advantage of this new class is as a unified error handling. If an error occurs in the SQL statement, so a nicely formatted error message is now displayed automatically and must no longer, as before, the function db_error be called manually. In addition, the call tracking is improved. It will now be logged each script, and each row specified in which the error has occurred, based on the called script of the user.

In addition to the error handling now also a logging of the SQL Views is installed. If the Admidio-debug flag is set and in the php.ini the PHP logging is enabled, you can now see in PHP logfile all SQL statements. Thus you can check the incorrect SQL statements and check which scripts have quite a lot of database accesses.

Another advantage of the new classes is the easy connection to other databases. With relatively little effort Admidio can later also run on other databases.

In the common.php a global object $gDb is defined for that class again, which can only access a MySQL database at the moment by default. The individual method calls are based on the previous MySQL function calls but only the mysql_ is missing before.

In code, the change looks like this:
Previous Code

$sql    = 'SELECT * FROM '. TBL_DATES;
$datesResult = mysql_query($sql, $g_adm_con);
db_error($datesResult, __FILE__, __LINE__);
while ($row = mysql_fetch_array($datesResult))

New Code

$sql    = 'SELECT * FROM '. TBL_DATES;
$datesResult = $gDb->query($sql);
while ($row = $gDb->fetch_array($datesResult))

The Connection-ID must not be handed over, as this is managed in the class. Otherwise, all handovers are remained as it was in the original MySQL functions.

If in the loop any additional SQL statements are executed, the resource ID(result) may not be transferred. Internally, the last will be automatically used.

  • en/entwickler/datenbankzugriffsklasse.txt
  • Last modified: 2016/12/03 15:13
  • by ximex