Hongmu Notes
Home Program Notes The WordPress database was successfully updated, yet when a query is executed, the data still reverts to its state before the update – why is this happening?
Program Notes wordpress

The WordPress database was successfully updated, yet when a query is executed, the data still reverts to its state before the update – why is this happening?

The WordPress database was successfully updated, yet when a query is executed, the data still reverts to its state before the update – why is this happening?

This issue is typically caused by the caching mechanism used in WordPress. When you perform an update operation in WordPress, the $wpdb object stores the data in memory; when the same data is queried again, it retrieves the data from memory instead of fetching it from the database. While this caching mechanism can improve query performance, it may also lead to the situation where data does not update immediately after an update is performed.

To resolve this issue, you can force a refresh of the $wpdb object's cache after updating the data, ensuring that the next query retrieves the latest data from the database. In WordPress, you can achieve this using the `flush()` method. Here is an example:

<?php
// 引入WordPress的核心文件
require_once( dirname( __FILE__ ) . '/wp-load.php' );

// 更新数据
global $wpdb;
$table_name = $wpdb->prefix . 'my_table'; // 获取表名
$data = array(
    'name' => 'John',
    'email' => 'john@example.com'
);
$where = array( 'id' => 1 );
$wpdb->update( $table_name, $data, $where );

// 刷新缓存
$wpdb->flush();

// 查询数据
$results = $wpdb->get_results( "SELECT * FROM $table_name WHERE id = 1" );

// 输出查询结果
if ( $results ) {
    $row = $results[0];
    echo $row->name . ': ' . $row->email;
} else {
    echo 'No results found';
}

In this example, after updating the data, we call the `flush()` method on the `$wpdb` object to force a cache refresh. Then, we execute the query again and output the query results. Now, you should be able to see the latest updated data.

Please note that refreshing the cache may impact performance, as it forces the $wpdb object to fetch the latest data from the database. If your query operations are frequent and you have high performance requirements, it is recommended to use the caching mechanism provided by WordPress to reduce the number of database accesses.

微信赞赏

WeChat

支付宝赞赏

Alipay

✍️ Author: Hong Mu

webmaster · Thanks for reading, stay tuned for more exciting content

Author homepage View home page →

Related articles

What should I do if the Redis Object Cache plug-in cannot be linked after wordpress redis changes the password?

What should I do if the Redis Object Cache plug-in cannot be linked after wordpress redis changes the password? Program Notes wordpress

Problem description: My wordpress site server uses pagoda panel, installed php8.0, and installed redis on the extension panel, and installed Redis Object Cache plug-in in wordpress. In fact, in general, you don't need to do anything, just start it directly. However, it is unsafe for radis not to set a password, which is a risky loophole, so I still think it is better to set a password, …
👁 256
How do I disable revisions and automatic draft saving in WordPress?

How do I disable revisions and automatic draft saving in WordPress? Program Notes wordpress

WordPress's automatic article revision tracking feature logs every time you edit an article in the后台; each revision is recorded as a separate entry in the `wp_posts` table. Due to the interplay between article revisions and automatic saving, the article ID often grows larger over time. While this typically does not cause significant issues for your WordPress installation, an excessive number of article versions can place a considerable burden on your storage space and database...
👁 373
Detailed Tutorial on Migrating EmpireCMS Blog Data to WordPress

Detailed Tutorial on Migrating EmpireCMS Blog Data to WordPress Program Notes wordpress

PS1: Second revision – now supports data transfer between categories, posts, and tags. PS2: If the post IDs consistently don't match, you can try clearing the WordPress post category data table and relationship tables. I previously used Typecho for my blog, but later migrated to Empire; however, Empire has too many features – using it for a blog feels like putting a large hammer into a small hole! Additionally, I often record important code snippets in my blog – all of which I've painstakingly written...
👁 546
WordPress issue: pages freezing due to image transcoding

WordPress issue: pages freezing due to image transcoding Program Notes wordpress

Last night, while uploading an image, for some reason, the uploaded image was automatically transcoded. Transcoding type: BASE64 encoding. For an image on a website, simply upload it to your server and then access the link – the image will then be displayed. However, there is another approach: directly transcoding the image; then you can simply access the converted image using its encoded URL. Problem description: When an image is relatively small (e.g., only a few KB in size), transcoding it and then hosting it on a website can help avoid...
👁 152
WordPress database query operations

WordPress database query operations Program Notes wordpress

To connect to a database for a WordPress installation, you need to include the `wp-config.php` file in your PHP script. This file contains the database configuration details for WordPress installation and other constants. Here is a simple example demonstrating how to include the `wp-config.php` file and establish a database connection: // Include the wp-config.php file: require...
👁 378
WordPress MySQL Database Table and Subtable Structure Functionality Guide

WordPress MySQL Database Table and Subtable Structure Functionality Guide Program Notes wordpress

WordPress uses a MySQL database. Its data table structure differs slightly from that of conventional databases; data can be stored either in the form of standard data tables or as data elements – the latter approach is less intuitive when viewed directly. To become a developer, you must understand the basic structure of the WordPress database and be able to use it within your own plugins or themes to write to or read from the database. I. Data Tables – As of WordPress...
👁 374

Recommended reading

(Adaptive Mobile Version) pbootCMS – Restaurant & Snack Chain Website Template; HTML5 Korean Cuisine Franchise Website Source Code Download – 0798

(Adaptive Mobile Version) pbootCMS – Restaurant & Snack Chain Website Template; HTML5 Korean Cuisine Franchise Website Source Code Download – 0798 Practical Collection pbootcms Template

A PbootCMS website template designed for a restaurant and snack chain business or a Korean cuisine franchise operation, compatible with both PC and WAP devices. Its modern and visually appealing design effectively showcases menu items, store ambiance, and franchise benefits, helping catering brands attract franchisees and consumers online. Template Preview | Installation Instructions | Website Backend: /admin.php | Username: admin | Password: admin | Extraction Password: www.4s5.cn | Related Articles...
👁 30
Empire cms website SEO optimization promotion template free download and sharing 001

Empire cms website SEO optimization promotion template free download and sharing 001 Practical Collection empire template

Templates are collected online, but there are problems with the templates. They have been repaired and the absolute paths and invalid css files have been reorganized and imported. After uploading, the installation is ready for use. It can be called according to your own website. There are two terminals, mobile terminal and PC terminal. The default background admin and 123456 rendering video display
👁 262
Industrial Mechanical Tofu Making Machine Equipment Website Template 0500

Industrial Mechanical Tofu Making Machine Equipment Website Template 0500 Practical Collection Yiyou template

This EyouCMS template is ideal for industries involving industrial machinery and tofu-making equipment. Its professional and practical design is perfect for showcasing tofu-making machines, industrial machinery and equipment, technical specifications, and food processing applications. It helps food machinery companies showcase their products online and attract potential clients in the food processing sector. Template Overview | Installation Instructions | Website Backend: /login.php | Username: admin | Password: admin | Related Articles: Common Questions About EyouCMS Installation...
👁 54
(PC + WAP) pbootCMS Atmospheric Environmental Protection News Website Template – Download Blue Association Website Source Code: 0799

(PC + WAP) pbootCMS Atmospheric Environmental Protection News Website Template – Download Blue Association Website Source Code: 0799 Practical Collection pbootcms Template

A blue style atmospheric environmental protection information news website template, developed based on PbootCMS, supports PC and WAP access. This template is very suitable for environmental protection associations, government departments or news and information sites. The page layout is broad and the information level is clear. It integrates multiple functions such as article list, headline recommendation, policy and regulation display, etc. It is an efficient solution for building an environmental information release and industry communication platform. Template display Installation instructions Website backend...
👁 56
Responsive Hardware Accessories – Electromechanical Website Template 0501

Responsive Hardware Accessories – Electromechanical Website Template 0501 Practical Collection Yiyou template

An eyouCMS responsive website template designed for the hardware and electromechanical industries. Its professional and sophisticated design is ideal for showcasing hardware components, electromechanical equipment, technical specifications, and industrial applications. This template helps hardware and electromechanical enterprises showcase their products online and attract industrial customers. Template Display | Installation Instructions | Website Backend: /login.php | Username: admin | Password: admin | Related Articles: Summary of Common Installation Issues for EasyCMS...
👁 52
(PC + WAP) pbootCMS Wood Industry Website Template – Download source code for a green wood processing enterprise website – 0800

(PC + WAP) pbootCMS Wood Industry Website Template – Download source code for a green wood processing enterprise website – 0800 Practical Collection pbootcms Template

A PbootCMS template featuring a green-themed design, specifically tailored for wood processing and wood industry enterprises, supporting both PC and WAP mobile devices. Its natural design style effectively highlights the organic texture of wood products. The template includes built-in common modules such as product display, company profile, and news updates, enabling wood companies to quickly set up their official websites and enhance the effectiveness of their online marketing campaigns. Template Display | Installation Instructions | Website Backend: /admin.php...
👁 48