Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

$wpdb->insert not working, last_query doesn't show insert "SHOW FULL COLUMNS FROM"

Tags:

I run $wpdb->insert($table, $data) where data is an array with column_name => value and the insert is not working. I tried $wpdb->last_query and something bizarre comes back:

SHOW FULL COLUMNS FROM `table_im_trying_to_insert` 

Why is the last query not my insert?

like image 554
ecorvo Avatar asked Apr 29 '15 01:04

ecorvo


People also ask

Where is Wpdb defined in WordPress?

By default, the $wpdb variable is an instance of the wpdb class that connects to the WordPress database defined in wp-config. php . If we want to interact with other databases, we can instantiate another instance of wpdb class.

How do I find the last insert ID in WordPress?

If you want to get the last inserted row ID from the WordPress database. You can use the $wpdb->insert() it does the insert. $lastid = $wpdb->insert_id; You can find more information about how to do things the WordPress way can be found in the WordPress codex.

How do I find the value of a WordPress database?

get_var # In many cases, you will need only one value from the database; for example, the email address of one of your users. In this case, you can use get_var to retrieve it as a simple value. The value's data type will be the same as its type in the database (i.e. integers will be integers, strings will be strings).

How do I use a database query in WordPress?

Below is an example of querying the database for posts within a category using WP_Query class. $query = new WP_Query( 'cat=12' ); The result will contain all posts within that category which can then be displayed using a template. Developers can also query WordPress database directly by calling in the $wpdb class.


2 Answers

I found the problem. Apparently with the new WP update if you try to insert into a VARCHAR column and the column length is less than what you are trying to insert it just won't work. Prior to this update it will insert it but trim off the excess characters.

like image 155
ecorvo Avatar answered Oct 17 '22 09:10

ecorvo


For me, I changed the field type from VARCHAR to TEXT, but it still didn't work. Finally, I found the table with collation of utf8_general_ci does not support emoji, so I removed all the emojis from the content then it works.

like image 27
micmia Avatar answered Oct 17 '22 07:10

micmia