I am trying to insert data into a custom table. I am using a mysql db with phpmyadmin in my backend. However, I get the following error:

[28-Jun-2018 19:36:05 UTC] WordPress database error  for query INSERT INTO `wp_ticker` (`coin_id`, `price`, `volume_24h`, `market_cap`, `percent_change_1h`, `percent_change_24h`, `percent_change_7d`, `created_at`, `updated_at`) VALUES ('2856', '0.00469682', '1471270', '135309340', '0.56', '-0.45', '-22.2', '2018-06-28 19:36:05', '2018-06-28 19:36:05') made by do_action_ref_array, WP_Hook->do_action, WP_Hook->apply_filters, call_user_func_array, Market->updateMarket

The structure of the table looks like the following:

CREATE TABLE {$wpdb->prefix}ticker (
    id bigint(20) NOT NULL AUTO_INCREMENT,
    coin_id bigint(20) NOT NULL,
    price longtext NOT NULL,
    volume_24h bigint(20) NOT NULL,
    market_cap bigint(20) NOT NULL,
    percent_change_1h DECIMAL(15,8) NOT NULL,
    percent_change_24h DECIMAL(15,8) NOT NULL,
    percent_change_7d DECIMAL(15,8) NOT NULL,
    created_at datetime NULL,
    updated_at datetime NULL,
    PRIMARY KEY (id),
    FOREIGN KEY (coin_id) REFERENCES {$wpdb->prefix}coins(id)

I am preparing my data like the following:

        $resTicker = array(
            'coin_id' => intval($coin_id),
            'price' => $tick->price,
            'volume_24h' => $tick->volume_24h,
            'market_cap' => floatval($tick->market_cap),
            'percent_change_1h' => floatval($tick->percent_change_1h),
            'percent_change_24h' => floatval($tick->percent_change_24h),
            'percent_change_7d' => floatval($tick->percent_change_7d),

        //check if the above ticker record exists already in the db
        $recordTickerExists = $wpdb->get_results(
             "SELECT * FROM {$wpdb->prefix}ticker
                 price = %s
                 AND coin_id = %s 
                 AND market_cap = %s 
             LIMIT 1",
             floatval($tick->price), $coin_id, floatval($tick->market_cap)

        if ( $recordTickerExists == 0 || $recordTickerExists == null ) {
            // ticker does not exists else do nothing
            try {
                $resTicker['created_at'] = date('Y-m-d H:i:s');
                $resTicker['updated_at'] = date('Y-m-d H:i:s');
                $wpdb->insert("{$wpdb->prefix}ticker", $resTicker);
            } catch (\Exception $ex) {
              // ...  

Any suggestions what I am doing wrong or see further information on the error?

I appreciate your replies!

  • 1
    Why do you use coin_id and market_cap as string in your select query? – Krzysiek Dróżdż Jun 28 '18 at 20:43
  • 1
    have you tried running generated query in PHPMyAdmin? wpdb doesn't throw exceptions, it has its own error handling mechanism – Tom J Nowell Jun 29 '18 at 1:16
  • @TomJNowell Yes, I ran the insert query in phpmyadmin and the weird thing is that it does not throw any exception back there... – Kare Jun 29 '18 at 4:39

You're inserting a float into market_cap, which is of bigint type. Doesn't seem to be the thing that would cause the issue in this case, but you might want to use intval() instead of floatval() or change the field to one that'll handle a float.

| improve this answer | |
  • Thank you for your answer! However, I still get the above error. Any suggestions how to find out further information? – Kare Jun 30 '18 at 9:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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