The question:
In order of figuring out the following issue, see https://wordpress.stackexchange.com/questions/178995/sanitize-a-working-query-string-by-using-wpdb-prepare-fails-with-mysql-db-er i ran into an rather odd behaviour. Even that my used query was correct and showing the right output.
global $wpdb;
$wpdb->show_errors();
$pageposts = $wpdb->get_results(
$wpdb->prepare(
"
SELECT skposts.*
FROM $wpdb->posts skposts,
$wpdb->postmeta skpostmeta1,
$wpdb->postmeta skpostmeta2
WHERE skposts.ID = skpostmeta1.post_id
AND skposts.ID = skpostmeta2.post_id
AND skpostmeta1.meta_key = %s
AND skpostmeta2.meta_key = %s
AND skposts.post_type = %s
AND skposts.post_status = %s
ORDER BY skpostmeta1.meta_value ASC,
skpostmeta2.meta_value ASC
",
'class_day',
'class_start',
'courses',
'publish'
)
, OBJECT
);
$wpdb->print_error();
Never the less as long as i have show_errors
and print_error
active i get an WP database error output alongside:
WordPress database error: []
SELECT skposts.* FROM hmjtZ_posts skposts, hmjtZ_postmeta skpostmeta1, hmjtZ_postmeta skpostmeta2 WHERE skposts.ID = skpostmeta1.post_id AND skposts.ID = skpostmeta2.post_id AND skpostmeta1.meta_key = 'class_day' AND skpostmeta2.meta_key = 'class_start' AND skposts.post_type = 'courses' AND skposts.post_status = 'publish' ORDER BY skpostmeta1.meta_value ASC, skpostmeta2.meta_value ASC
But why? I would have expected that an output is shown only if something goes wrong like an error or warning but this way something is shown all the time.
The Solutions:
Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.
Method 1
The output that you posted above is expected behaviour for $wpdb->print_error()
if the following is true –
- You are running a single site, not multisite
$wpdb->suppress_errors
is set to false$wpdb->show_errors
is set to false
From the looks of your code, you meet all those conditions.
Note also that, unless you have turned them off previously, $wpdb->show_errors
is set to true
by default, so you don’t need to call $wpdb->show_errors()
.
To output something only when there is a DB error you can do one of these two things –
1 – Output the error and add the error to the log
As well as outputting on the screen, the $wpdb->print_error()
method will log your error. If this is desirable behaviour (recommended), you can do this –
if($wpdb->last_error !== '') :
$wpdb->print_error();
endif;
2 – Output the error but do not log it
If you are not interested in logging the error, you can add your own my_print_error()
funciton and use that instead of $wpdb->print_error()
–
function my_print_error(){
global $wpdb;
if($wpdb->last_error !== '') :
$str = htmlspecialchars( $wpdb->last_result, ENT_QUOTES );
$query = htmlspecialchars( $wpdb->last_query, ENT_QUOTES );
print "<div id='error'>
<p class='wpdberror'><strong>WordPress database error:</strong> [$str]<br />
<code>$query</code></p>
</div>";
endif;
}
Last Edit: Syntax Mistake
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0