0

In some cases, when given a bad query, will attempt to run the query, and give a warning (instead of an error). In the MySQL prompt, to see these warnings, you can run SHOW WARNINGS;.

For instance:

mysql> SELECT * FROM wp_posts WHERE id = "hello";
Empty set, 1 warning (0.00 sec)

mysql> SHOW WARNINGS;
+---------+------+-------------------------------------------+
| Level   | Code | Message                                   |
+---------+------+-------------------------------------------+
| Warning | 1292 | Truncated incorrect DOUBLE value: 'hello' |
+---------+------+-------------------------------------------+
1 row in set (0.00 sec)

However, if I run PHP code like this, there is no way to detect this MySQL warning:

$results = $wpdb->get_results('SELECT * FROM wp_posts WHERE id = "hello"');

$wpdb->show_errors and $wpdb->print_error does not seem to detect MySQL warnings.

How can I detect the MySQL warnings?

0

From quick look through the source it doesn't seem like wpdb actively implements any handling for warnings (as opposed to errors).

Proactively you can just ask for them as a custom query ($wpdb->get_results( 'SHOW WARNINGS;' ) I suppose, but implicitly they just aren't tracked by WP core.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.