WordPress $wpdb->query() does not return

109
February 20, 2019, at 10:30 AM

After successfully executing two SQL DROP TABLE statements using $wpdb->query(), $wpdb->query() never returns from the third call. Below is the sample PHP code and the lines from the debug.log. I have been unable to find any documentation either here or at wordpress.org where someone else has reported a similar issue.

If I insert a DROP VIEW IF EXISTS statement before the third DROP TABLE, the DROP VIEW IF EXISTS executes and returns and when the third DROP TABLE is called $wpdb->query() never returns.

If I insert a second DROP VIEW IF EXISTS statement before the third DROP TABLE, both DROP VIEW IF EXISTS execute and return and when the third DROP TABLE is called $wpdb->query() never returns.

I have rearranged the DROP TABLE statements. It makes no difference which one is the third DROP TABLE.

If I execute one DROP TABLE IF EXISTS containing all three table names, it works fine.

$table_name1 = $wpdb->prefix . "Codes";
$table_name2 = $wpdb->prefix . "Phrases";
$table_name3 = $wpdb->prefix . "Exceptions";
$SQL = "DROP TABLE IF EXISTS $table_name1, $table_name2, $table_name3;";
$wpdb->query($SQL);

Dropping three tables in one statement is a satisfactory workaround but I am curious about why when I execute two DROP TABLE statements, a third one hangs. Have you seen this behavior before? Do you know why this occurs? Is it remedied on a newer software version?

This is the software suite I am to use in this engagement: WAMP Server:

  • Apache 2.4.23
  • MySQL 5.7.14
  • PHP 7.0.10
  • WordPress 5.0.3

Sample code:

function Drop_Tables() {
    global $wpdb;
error_log("begin");
    $table_name = $wpdb->prefix . "Codes";
    $SQL = "DROP TABLE IF EXISTS $table_name;";
    $wpdb->query($SQL);
    $xresult=$wpdb->last_error; 
    if ($xresult) {
        error_log($xresult);
    }
error_log("After Drop Table Codes");
    $table_name = $wpdb->prefix . "Phrases";
    $SQL = "DROP TABLE IF EXISTS $table_name;";
    $wpdb->query($SQL);
    $xresult=$wpdb->last_error; 
    if ($xresult) {
        error_log($xresult);
    }
error_log("After Drop Table Phrases");
    $table_name = $wpdb->prefix . "Exceptions";
    $SQL = "DROP TABLE IF EXISTS $table_name;";
error_log("Before query()");
    $wpdb->query($SQL);
error_log("After query()");
    $xresult=$wpdb->last_error; 
    if ($xresult) {
        error_log($xresult);
    }
error_log("After Drop Table Exceptions");
}

From debug.log:

[19-Feb-2019 13:16:54 UTC] begin
[19-Feb-2019 13:16:54 UTC] After Drop Table Codes
[19-Feb-2019 13:16:54 UTC] After Drop Table Phrases
[19-Feb-2019 13:16:54 UTC] Before query()
Rent Charter Buses Company
READ ALSO
Mysql - Count rows until a first distinct value is reached

Mysql - Count rows until a first distinct value is reached

I am trying to count the number of rows until the first occurrence of distinct value, for every distinct value

130
I need specific table data for each users

I need specific table data for each users

I am doing my master thesis and I found unknown problem

136
Three tables association

Three tables association

 I have three (3) tables that rely on each other in order to retrieve the correct taxes for a product typeBellow I'll describe the tables and their functionality in detail:

129