Avoiding “Usage of a direct database call is discouraged”

Using $wpdb->insert and or related methods to modify data within any of the default WordPress tables, be it posts, postmeta, user, usermeta etc is discouraged because there are functions which already exist for the purpose of modififying data within those tables. For example, wp_insert_post wp_update_post wp_delete_post Database Queries Avoid touching the database directly. If there … Read more

get_results on large datasets

$wpdb doesn’t suit for fetching huge amount of data from database. Why? In your case: $wpdb->get_results( … ) – fetches all results into your RAM at once. It means if you have 4mb, 10mb, or 50mb of data in db, everything will be stored in memory (what is limited as you know). $wpdb->get_results( …, ARRAY_A … Read more

WPDB prepare – like % – placeholders?

Yes, they’re normal. They were added in 4.8.3 to fix an SQL injection vulnerability. You can read an article describing the technical reasons for this happening here and the ticket for the change here. The placeholder characters are replaced by the random characters on the last line of $wpdb->prepare() with the $wpdb->add_placeholder_escape() function, which calls … Read more

$wpdb->insert not working in any way

When $wpdb method doesn’t perform as it should it is likely issue with resulting SQL query (because of wrong input or something else). Follow wpdb reference in Codex for troubleshooting: enable database error display via $wpdb->show_errors() check what query is being formed and run via $wpdb->last_query