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?
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.
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.
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).
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With