I had an sql query that wasn't executing properly when initiating the plugin. So I tried to debug it by
var_dump
ing the sql and exit
ing, which causes the plugin's output to display with a fatal error message on the wordpess plugin activation screen.At least, that's what it does normally. My code looked like this:
echo 'gh1';
if($wpdb->get_var("SHOW TABLES LIKE '$tblCur'") != $tblCur) {
echo 'gh1a';
//create the table
$sql = 'CREATE TABLE IF NOT EXISTS '.$tblCur.' (
code VARCHAR(3) NOT NULL,
symbol VARCHAR(5) NOT NULL,
name VARCHAR(25) NOT NULL,
symbol_entity_num VARCHAR(30) NOT NULL,
symbol_entity_hex VARCHAR(30) NOT NULL,
rate FLOAT(7) UNSIGNED NOT NULL,
PRIMARY KEY (code)
)';
echo 'gh2a';
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
echo 'gh3a';
dbDelta($sql);
echo 'gh4a';
$sql= "INSERT INTO $tblCur (code, symbol, name, symbol_entity_num, symbol_entity_hex) VALUES ";
echo 'gh5a';
if (($handle = fopen(WP_PLUGIN_DIR.'/wp_dk_currencyConversion/currency_details.csv', 'r')) !== FALSE) {
$data = fgets($handle);
echo 'gh6a';
$sql.= "($data)";
while (($data = fgets($handle)) !== FALSE) {
$sql.= ", ($data)";
}
fclose($handle);
}
echo 'gh7a';
$wpdb->query($sql);
echo 'gh8a';
var_dump($sql); exit();
}
echo 'gh2';
if($wpdb->get_var("SHOW TABLES LIKE '$tblCou'") != $tblCou) {
echo 'gh1b';
//create the table
$sql = 'CREATE TABLE IF NOT EXISTS '.$tblCou.' (
country VARCHAR(2) NOT NULL,
currency VARCHAR(3) NOT NULL,
PRIMARY KEY (country, currency)
)';
echo 'gh2b';
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
echo 'gh3b';
dbDelta($sql);
echo 'gh4b';
$sql= "INSERT INTO $tblCou VALUES ";
echo 'gh5b';
if (($handle = fopen(WP_PLUGIN_DIR.'/wp_dk_currencyConversion/country_currency_map.csv', 'r')) !== FALSE) {
$data = fgets($handle);
echo 'gh6b';
$sql.= "($data)";
while (($data = fgets($handle)) !== FALSE) {
$sql.= ", ($data)";
}
fclose($handle);
}
echo 'gh7b';
$wpdb->query($sql);
echo 'gh8b';
var_dump($sql); exit();
}
echo 'gh3';
Strangely, the SQL within the first if block would be executed (the table would be created), but none of the echo
es, the var_dump
within that block would be printed, and the exit
in that block would not execute either.However, all the other echo statements, including the ones in the 2nd if block, would be printed, and the
exit
in the 2nd if block was honoured.After fixing the sql error (I had an extra column I was missing), I tried putting
var_dump($sql); exit();
straight after the first if block. Again, the sql in the first if block was executed, but no output from that block was recorded, and the exit statement in the block wasn't honoured.The script now exited where my exit statement between the two if blocks was, but the $sql variable was NULL, as if the first if block hadn't been executed at all.
The only reasoning I can think of behind this is that if a plugin exits, Wordpress may try activating the plugin again, and only report a fail and the output of the plugin when the second attempt exits.
This would explain it as the table wouldn't exist on the first try, and so it would exit after creating the table. Wordpress would then try again, and the table would now exit, so none of the first if block would be executed on this second attempt, which would then exit and only print data from the second attempt.
As an aside, unfortunately it seems that neither setting
$wpdb->show_errors()
and using $wpdb->print_error()
actually do anything when a mysql query fails. This is why I use var_dumps of the sql instead - I can then copy the sql and try it in phpMyAdmin, which will then give me the error why it failed.In the afternoon I also topped up the pond a bit.
In the evening I also watched an episode of Magi Rangers and Let's Learn Japanese Basic 1 with Mauser and Bo.
No comments:
Post a Comment