Has anyone converted $wpdb to a standalone generic database handling class?

After using WordPress and $wpdb for a while, I’ve come to really appreciate the clean and simple way they handle database queries.

Before I try to reinvent the wheel, has anyone converted the $wpdb class into a standalone PHP class that I can use with any MySql database?

Read More

A couple of Google searches didnt return any results.

Related posts

Leave a Reply

2 comments

  1. Looks like a Richard Racko has done just that (latest commit 14 Feb 2018 at time of writing):

    https://github.com/richardracko/wpdb-standalone

    From Richard’s limited doco for wpdb-standalone at github:

    WordPress standalone database class cleaned up from WP specific
    variables and function calls

    Also, looks like it might be possible to create a zero length wp-settings.php file, and thus re-use an existing wp-config.php just for settings without needing to edit it to remove the wp-settings.php reference:

    require 'wpdb.php';
    // create a zero length wp-settings.php to avoid editing wp-config.php
    require 'wp-config.php';
    $wpdb = new wpdb(DB_USER, DB_PASSWORD, DB_NAME, DB_HOST);
    //$wpdb = new wpdb($dbuser, $dbpassword, $dbname, $dbhost);
    

    While it’s possible to use the SHORTINIT method to include the standard wp-db.php, from my reading some time back that loaded all plugins. The syntax of ezdb, the original class wp-db is based on, is a litle different from wp-db, though it could be an option as well.

    I used this in a small project and it has worked for a simple use case so far. Note that it doesn’t implement $wpdb->prefix but you can use $table_prefix as set in the wp-config.php file.

    Worth noting that this version still has the horrible bug where an update fails with no error if a value exceeds column length; that is, if you try to save 60 bytes in a text field of 50, it will fail silently with only the result giving any clue it hasn’t worked. There is a patch around for this that is worth having on your development site.