How to execute mulitple statement SQL queries using $wpdb->query?

TL;DR This is not possible with wpdb. Use dbDelta(). Even better, use something else to run raw queries. Explanation wpdb->query() uses the mysql_query() function. From PHP manual: mysql_query() sends a unique query (multiple queries are not supported) to the currently active database on the server that’s associated with the specified link_identifier. In order for that … Read more

Notice: wpdb::prepare was called incorrectly. The query argument of wpdb::prepare() must have a placeholder

You don’t need to remove the prepare(), but you just need to do it properly. And please check the $wpdb->prepare() reference for the function’s syntax etc., but basically, instead of (wrapped for brevity): $insert_query = “INSERT INTO `”.$computer_repair_items.”` VALUES(NULL, ‘”.$wc_service_name.”‘, ‘services’, ‘”.$post_id.”‘)”; $wpdb->query( $wpdb->prepare($insert_query) ); you should do it like this: (presuming $computer_repair_items is a … Read more

Should close $wpdb via $wpdb->close()

The close() function was added for completeness in the database abstraction. If the database is prematurely closed in a plugin or theme (as you are using it), the next call to wpdb::query() will re-open the connection. Your Commands out of sync error is a MySQL error, not a WordPress one, and it might be coming … Read more

if statement on database query

On your if(), it should be like this: if (!($wpdb->get_row(“SELECT voter_ip FROM ” . $wpdb->prefix . “voter_ips WHERE post_id = $post_id AND voter_ip = ‘$voter_ip'”) Notice the quotation mark added on $voter_ip.

WP database error for comments_popup_link()

Your template code includes $wpdb->print_error(). This function prints the last database error between [ and ] brackets, and the executed SQL code. But if there is no error, you just see the empty brackets and the SQL. $wpdb->show_errors() is used to enable displaying of database errors. If you want to see all database errors, you … Read more

Safe way to find last inserted id in a table?

At a higher level, there truly isn’t any means to know if the php/db connector will return the correct id by relying on $wpdb->insert_id. The only way to be 100% sure is to have add a key you know will be unique (and indexed as such); you can then retrieve the id by querying the … Read more