18

Got the lower portion here sorted, but now there's an issue when it inserts the record. I have the NULL value for file formatted as %s for string, but it's not inserting NULL it's inserting [BLOB - 0B]. The column format in the mySQL table is longblob. Any ideas?


this is my first time using $wpdb->insert and it looks like I've missed something.

Here's the loop I'm trying to use. There are currently 2 timestamps in the array.

for ( $i = 0; $i < count($timestamps); $i++ ) {
$working_time = $timestamps[$i];
$working_form = $formnames[$i];

$status_data = array(
    'submit_time' => $working_time,
    'form_name' => $working_form,
    'field_name' => 'lead_status',
    'field_value' => 'new',
    'field_order' => 10001,
    'file' => NULL
);
$status_data_types = array(
    '%f',
    '%s',
    '%s',
    '%s',
    '%d',
    '%s'
);

$result = $wpdb->get_results("SELECT field_value FROM ".$leadtable." WHERE submit_time = ".$working_time);

if(!$result) {              
    $insert = $wpdb->insert($leadtable, $status_data, $status_data_types);
    if( !$insert ) {
            echo 'didn\'t work';
        }
}
}

I know that $working_time and $working_form are both set properly. $working_time is a long float like 1387175380.9600 and $working_form is a string.

Nothing is being returned, even by the if( !$insert ) check so I'm guessing it's erring somewhere before that point. I know that if( !$result ) will return true because that data does not exist yet.

Found the issue. The get_results query was failing due to a missed period. My HS English teacher was right... a missed period can be a HUGE problem!

0

4 Answers 4

26

If you want to get the last error and last query you can use this properties of $wpdb object:

$wpdb->last_error 

will show you the last error, if you got one.

$wpdb->last_query 

will assist you with showing the last query (where the error occurred)

I hope this will help you out.

11

https://core.trac.wordpress.org/ticket/32315

One of the columns inputs may be larger than the column is. Wordpress detects this and will not even send the query to the DB.

The Diff there shows a patch for wp-db you can put in to get more information in the last_error message so it will not be empty.

enter image description here

2
  • Nice one Liam. Very annoying that WP does not have an error message for this block.
    – Joe
    Commented Oct 21, 2021 at 10:03
  • "One of the columns inputs may be larger than the column is. Wordpress detects this and will not even send the query to the DB." - This helped me with another issue, thank you!
    – Lee
    Commented May 23, 2023 at 9:41
10

Maybe your config hides error showing. Have you tried $wpdb->print_error();?

4
  • yeah, all over the place. after each insert statement and inside the if(!) statements. It's not returning anything. Commented Dec 16, 2013 at 8:05
  • added it in a couple other places tho, and, of course, found the error. Bad query. Thanks a bundle. Commented Dec 16, 2013 at 8:14
  • Wouldn't happen to know the data format call to insert data into a column formatted as blob would you? Needs a null value and %s for string isn't working. Commented Dec 16, 2013 at 8:22
  • Try to use exit() to force PHP to die, inside the function call place the output of print_error, there is no way that doesn't output anything nor dies Commented Dec 16, 2013 at 9:48
5

Hopefully, you have defined the global variable $wpdb.

If your $wpdb is not working, and also not showing any error then you must try these three steps.

  1. Print your error using with errors functions.

    echo $wpdb->last_error;

    or

    echo $wpdb->show_errors;

  2. If no error is visible then you must print your last query using with last query function.

    echo $wpdb->last_query;

  3. Copy and paste your last query in your Phpmyadmin > Your Database -> Table -> SQL tab, click on the Go button, and you will definitely get proper solutions(errors) in it.

3
  • show_errors() doesn't need an echo, it simply sets the class to show errors. Errors will appear when we activate this.
    – brasofilo
    Commented Jun 14, 2021 at 0:58
  • 1
    $wpdb->last_query is not a method, it's a property. See this entry in the [codex] (developer.wordpress.org/reference/classes/wpdb/#properties) Commented May 20, 2022 at 16:40
  • Hey @RafaelAtías, sorry it's my mistake, Yes, I just corrected it. Thank you. Commented May 22, 2022 at 5:27

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