• Hello, I have an old plugin that I wrote long ago that used mysql with wordpress 2.9.1. I’m trying to update it to use wordpress 5.2 with PHP 5.6 installed.

    My plugin did lots of sql queries, like this:

    $query = mysql_query("SELECT testtable, testquestionpage, testincorrectpage, testanswerpage, testdonepage FROM wp_test_index WHERE id='" . $testid . "';");

    They’re now all returning false instead of the query result. This is with PHP 5.6, which should support both mysql and mysqli. I have a couple questions:

    1) Do I need to convert from mysql to mysqli?

    2) Do I need to supply the link_identifier (optional second argument) to mysql_query? I notice the wordpress code in wp-db.php tends to supply $this->dbh when making mysql functions, but I’m assuming that’s a private member of the wpdb class. Is there a way for me to get the database handle and use it in my plugin?

    Thanks

Viewing 3 replies - 1 through 3 (of 3 total)
  • 1) Do I need to convert from mysql to mysqli?

    Yes, you will have to do that.

    https://github.com/philip/MySQLConverterTool

    Dion

    (@diondesigns)

    You technically don’t have to use mysqli if the plugin will only be used by you, and you never plan to update to PHP 7+. That’s a bad idea on many levels, so please use mysqli. Since you must add the db handle to your queries anyway, it won’t be much work to convert them to mysqli format. Note that WordPress defaults to mysqli with PHP 5.6; using mysql requires a special constant being defined in wp-config.php.

    You can use the following code to obtain the db handle from the $wpdb class:

    global $wpdb;
    $dbh = $wpdb->__get('dbh');

    I do this routinely in plugins when the bloat of the $wpdb class will excessively impact performance.

    Thread Starter smbaker

    (@smbaker)

    Thanks everyone, the plugin is working again! 😀

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘Converting an old plugin that used mysql_query’ is closed to new replies.