MyBB Community Forums

Full Version: Database Helper
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Plugin Name: Database Helper
Latest Version: 2.0
Uses Language: Yes
Languages Available: English
Download: https://community.mybb.com/mods.php?acti...w&pid=1083
Github: https://github.com/dragonexpert/database_helper

Installation:
1) Upload everything in the zip file to their respective directories.
2) Activate in the Admin CP.
3) View slow queries in Admin CP ->Tools & Maintenance -> Slow Query Log


Debugging:
If you have a page that is running slow, you might consider using search and replace to look for $db-> and change it to $db->helper-> in that file.  This will force any queries to go through the helper which will also log any query that takes longer than 2 seconds to execute.  You are then able to view the entry in the Admin CP and see how long it took for the query to execute.

Methods:
  • add_foreign_key - adds a foreign key. See function for parameters. Not supported in SQLite.
  • drop_foreign_key($name, $table) - Drops a foreign key.
  • get_foreign_keys - gets all foreign keys. ( Note only works on foreign keys created with add_foreign_key ).
  • count_query($table, $fields, $where) - performs a select count() and returns the result.
  • fetch_clean_array($resource, $resulttype = MYSQLI_ASSOC) - similar to fetch_array, but calls htmlspecialchars_uni on the result before returning it.
  • fetch_clean_field($resource, $field) - same as above except a field.
  • truncate_table($table) - Truncates a table.
  • setting_exists($setting) - Whether a setting exists.
  • log_slow_query($query, $execution_time) - Logs a query. Can be called manually if you provide the values.

Writing New Code:
The only difference you will need to make to your code is using $db->helper-> instead of just $db->.
I have a new version on Github that needs testing. The new version also logs the value of the constant THIS_SCRIPT and the name of the file that generated the query. These improvements will make it faster to find the problem.
It would be perfect if it would log front end error messages as well.
^ would you suggest merging of Error Viewer plugin code with this ?
Pretty much, yes .m.