I'm trying to update data into the db using $wpdb, but it's saving only 999 characters, I'm saving the data to a longtext field. Here's my snippet:

if ( $idExists ) {
    $tableOpr = $wpdb->update( 'my_table', array(
            'col_name' => $my_data,
        ),
        array (
            'id' => $idExists,
        )
    );
}

Kindly help me out with the solution.

Notes:

  1. $my_data is serialized array.
  2. Update works when $my_data has less than 999 characters.
  3. I tried using phpMyAdmin, and it worked there, the column 'col_name' is longtext.
  4. If I delete and enter data then it works, but update query only updates first 999 characters.
  • 1
    What's the code for creating the table? And where does $my_data come from? – Jacob Peattie Jul 2 at 2:52
  • 4
    If you show all your code (create table and examples of updates) then people can try recreate problem and may be better able to help. – anmari Jul 4 at 6:07
  • 2
    downvoted as it is extremely localized no where in wordpress there is any obvious SQL size limitation therefor it is something in your setup – Mark Kaplun Jul 6 at 13:36
  • 1
    Sometimes PHPMyAdmin shows data truncated. Are you sure they are not just truncated in your view of PHPMyAdmin? – Scott Jul 7 at 9:58
  • 1
    If all else fails, then make a totally fresh install of WordPress (i.e. no themes or plugins other than the default ones) and re-create your MySQL table and then try your code. If the problem persists, try with my suggestion (serialize( range() )) and if that works, then the problem is with the actual data you used. If not, then maybe you should talk to your hosting support... or add all of your code to your question. – Sally CJ Jul 9 at 11:09

I do not see any code function in soruce code which will strip string to 999 characters. I created simple example to proof that.

Create table.

CREATE TABLE test (
 id bigint(20) unsigned NOT NULL auto_increment,
 title text NOT NULL default '',
 PRIMARY KEY (id)
) DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

Inert sample row.

INSERT INTO test (title) VALUES ('Lorem ipsum');

Add code to your plugin.

/**
 * Function which will update row title with long value.
 */
function wpse_307455_update_to_long_title() {

    global $wpdb;

    $wpdb->update('test', array(
        'title' => str_repeat('Lorem ipsum ', 100),
    ), array (
        'id' => 1,
    ));

    $row = $wpdb->get_row('SELECT title FROM test WHERE id = 1'); 

    echo sprintf( 'Title length: %d', strlen( $row->title ) );
    die();
}

add_action( 'init', 'wpse_307455_update_to_long_title' );

This code should output something like:

Title length: 1200

My guess is that your column is not proper type. In my example I have used TEXT type which can contain 65 535 characters.

  • The question is about 999 characters in a single field, not about 999 rows. – Fayaz Jul 4 at 11:09
  • Sorry, I updated my answer. – kierzniak Jul 4 at 12:31

Deactivate all other plugins and switch to a standard theme, and then try your update again. OR try it in a separate simple clean install. From what you've said so far, it sounds like there is a filter on the wpdb query that may be affecting the update. If it works in a clean install, then you can start debugging to find out which plugin/theme function is affecting it.

  • sorry, but try this and try that are just not an answer, at best this is a comment – Mark Kaplun Jul 6 at 13:37
  • Fair enough, however based on OP's rejection of anything else offered in comments imho, the most likey reason is that there is code filtering the wp query - intended to limit field lengths for some other purpose and not realising that it is impacting more. Quickest way to verify that is to remove (deactivate) everything except vanilla wp and the OP's code. (Hmm unless it's in a must have plugin of course, that might still impact) – anmari Jul 9 at 1:53
  • 2
    to be fair I downvoted the question as well... it is just not answerable with anything which is more than a guess. – Mark Kaplun Jul 9 at 4:19

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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