I have these

    $wpdb->show_errors (true);

    $wpdbp = $wpdb->prepare($preparedQuery->query, $preparedQuery->values);
    $result = $wpdb->get_var($wpdbp);

I further thought of something like this (may be temporary)

    if($result === null) {
        $error = $wpdb->print_error();

I read in get_var() documentation:

Returns: Database query result (as string), or null on failure

My problem here is that $result is always null when inserting, even if insert succeeds. How to actually know it there were errors or not? I expect there may be some other types of get_ methods?

Maybe I sould add something like "SELECT 1;" but even if that works I don't like it much.

1 Answer 1


To insert data you should use query() (documentation), get_var() method is for selecting data. If error is encountered, query() returns FALSE.


This function returns an integer value indicating the number of rows affected/selected for SELECT, INSERT, DELETE, UPDATE, etc.

For CREATE, ALTER, TRUNCATE and DROP SQL statements, (which affect whole tables instead of specific rows) this function returns TRUE on success. If a MySQL error is encountered, the function will return FALSE.

$result = $wpdb->query( 'insert query' );
if ( $result === FALSE ) {
    // display error

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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