Functions used for wordpress database operations.
When using wordpress, if you want to directly use the database operation class (wp-db.php) encapsulated in WP, you can use it by including wp-blog-header.php in the code.
define(‘PATH’, dirname(dirname(__FILE__)).‘/’); require_once(PATH . ‘../wp-blog-header.php’); global $wpdb;
When inserting data, one of the methods is to use the insert() function in the wp-db class.
$table = "test_table"; $data_array = array( ‘column_1′ => ‘data1′, ‘column_2′ => ‘data2′ ); $wpdb->insert($table,$data_array);
The first parameter is the name in the database table, and the second parameter is the data to be inserted, which is an array. The name of the key in the array is the column name in the table. In fact, the insert() function also has a third parameter format. Interested friends can check in the method definition of wp-db.php. When updating data, you can use the update() function, for example:
$table = "test_table"; $data_array = array( ‘column_1′ => ‘new_data1′ ); $where_clause = array(
‘column_2′ => ‘data2′ ); $wpdb->update($table,$data_array,$where_clause);
To update data from the database There are many ways to get data, one of which is as follows:
$querystr = "SELECT column_1 FROM test_table"; $results = $wpdb->get_results($querystr); $i=0; while ($i< count($results)){ echo $results[$i]->column_1."<br />"; $i++; }
Query PHP syntax
<strong><?php $wpdb->query("DELETE FROM $wpdb->post WHERE post_id = ’13′ “); ?> </strong>
The parameter of query is any mysql statement. The return value is how many rows were selected and affected. Returns FALSE if an error occurs.
Select a variable
<strong><?php $wpdb->get_var('query',column_offset,row_offset); ?> </strong>
where query is the mysql statement to be queried. If it is empty, it means selecting it from the cache. column_Offset and row_offet indicate the column and row of the query return value, and the default value is zero. Typical usage is:
<?php $user_count = $wpdb->get_var($wpdb->prepare("SELECT COUNT(*) FROM $wpdb->users;"));?>
This SQL only selects one value. The default value is 0 rows and 0 columns, which means the number of users is selected. It's unclear why prepare is always added in front here.
Select a row
<strong><?php $wpdb->get_row('query', output_type, row_offset); ?> </strong>
query is the mysql statement to be executed, output_type indicates that the return value is object, hash or array; row_offset indicates the row.
By default, output_type is object.
$mylink = $wpdb->get_row("SELECT * FROM $wpdb->links WHERE link_id = 10");
echo $mylink->link_id; // prints "10"
if output_type=ARRAY_A , then:
$mylink = $wpdb->get_row("SELECT * FROM $wpdb->links WHERE link_id = 10", ARRAY_A); echo $mylink['link_id']; // prints "10"
Select a column
get_col('query',column_offset); ?>
Generally select
//$wpdb->get_results('query', output_type); <?php $fivesdrafts = $wpdb->get_results("SELECT ID, post_title FROM $wpdb->posts WHERE post_status = 'draft' AND post_author = 5"); foreach ($fivesdrafts as $fivesdraft) { echo $fivesdraft->post_title; }
Insert a row
<strong>//<?php $wpdb->insert( $table, $data, $format ); ?> <?php $wpdb->insert('table', array('column1' => 'value1', 'column2' => 123 ), array('%s','%d') ) ?></strong>
Update
//$wpdb->update( $table, $data, $where, $format = null, $where_format = null ); <?php $wpdb->update( 'table', array( 'column1' => 'value1', 'column2' => 'value2' ), array( 'ID' => 1 ), array( '%s', '%d' ), array( '%d' ) ) ?>
About wpdb prepare
As mentioned earlier, it is not clear why each mysql statement is included in prepare. Here is an explanation: Because the mysql statement may contain characters such as single quotes and double quotes, if it is not processed, it will be sent directly to mysql. , may cause errors. So here we use a prepare to preprocess the mysql statement. The syntax of prepare is:
$sql = $wpdb->prepare( 'query' [, value_parameter, value_parameter ... ] );
The query can contain %d, %S, and %f, which respectively indicate that the type of the following parameters is integer, character, and floating point. If you want to display the % sign, use %%. The syntax is basically the same as printf in C language.
That’s basically it. There should be no problem processing general databases. If you encounter problems, you can check it out in the article mentioned at the beginning of this article.
$wpdb is a global variable that contains multiple database query functions:
$wpdb -> get_results('query'); $wpdb->query('query'); $wpdb->get_var('query',column_offset,row_offset); $wpdb->get_row('query', output_type, row_offset); $wpdb->get_col('query',column_offset); $wpdb->get_results('query', output_type); $wpdb->insert( $table, $data, $format ); $wpdb->update( $table, $data, $where, $format = null, $where_format = null ); $wpdb->prepare( 'query' [, value_parameter, value_parameter ... ] ); $wpdb->show_errors(); $wpdb->hide_errors(); $wpdb->print_error(); $wpdb->get_col_info('type', offset); $wpdb->flush();
More: http://codex.wordpress.org/zh-cn:Class_Reference/wpdb
The above introduces the functions used in WordPress database operations. , including relevant content, I hope it will be helpful to friends who are interested in PHP tutorials.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Many users will choose the Huawei brand when choosing smart watches. Among them, Huawei GT3pro and GT4 are very popular choices. Many users are curious about the difference between Huawei GT3pro and GT4. Let’s introduce the two to you. . What are the differences between Huawei GT3pro and GT4? 1. Appearance GT4: 46mm and 41mm, the material is glass mirror + stainless steel body + high-resolution fiber back shell. GT3pro: 46.6mm and 42.9mm, the material is sapphire glass + titanium body/ceramic body + ceramic back shell 2. Healthy GT4: Using the latest Huawei Truseen5.5+ algorithm, the results will be more accurate. GT3pro: Added ECG electrocardiogram and blood vessel and safety

Why Snipping Tool Not Working on Windows 11 Understanding the root cause of the problem can help find the right solution. Here are the top reasons why the Snipping Tool might not be working properly: Focus Assistant is On: This prevents the Snipping Tool from opening. Corrupted application: If the snipping tool crashes on launch, it might be corrupted. Outdated graphics drivers: Incompatible drivers may interfere with the snipping tool. Interference from other applications: Other running applications may conflict with the Snipping Tool. Certificate has expired: An error during the upgrade process may cause this issu simple solution. These are suitable for most users and do not require any special technical knowledge. 1. Update Windows and Microsoft Store apps

In this article, we will show you how to reorder multiple columns in PowerQuery by dragging and dropping. Often, when importing data from various sources, columns may not be in the desired order. Reordering columns not only allows you to arrange them in a logical order that suits your analysis or reporting needs, it also improves the readability of your data and speeds up tasks such as filtering, sorting, and performing calculations. How to rearrange multiple columns in Excel? There are many ways to rearrange columns in Excel. You can simply select the column header and drag it to the desired location. However, this approach can become cumbersome when dealing with large tables with many columns. To rearrange columns more efficiently, you can use the enhanced query editor. Enhancing the query

Part 1: Initial Troubleshooting Steps Checking Apple’s System Status: Before delving into complex solutions, let’s start with the basics. The problem may not lie with your device; Apple's servers may be down. Visit Apple's System Status page to see if the AppStore is working properly. If there's a problem, all you can do is wait for Apple to fix it. Check your internet connection: Make sure you have a stable internet connection as the "Unable to connect to AppStore" issue can sometimes be attributed to a poor connection. Try switching between Wi-Fi and mobile data or resetting network settings (General > Reset > Reset Network Settings > Settings). Update your iOS version:

ReactQuery database plug-in: Methods to implement data import and export, specific code examples are required. With the widespread application of ReactQuery in front-end development, more and more developers are beginning to use it to manage data. In actual development, we often need to export data to local files or import data from local files into the database. In order to implement these functions more conveniently, you can use the ReactQuery database plug-in. The ReactQuery database plugin provides a series of methods

php提交表单通过后,弹出的对话框怎样在当前页弹出php提交表单通过后,弹出的对话框怎样在当前页弹出而不是在空白页弹出?想实现这样的效果:而不是空白页弹出:------解决方案--------------------如果你的验证用PHP在后端,那么就用Ajax;仅供参考:HTML code

This article will introduce how to use PowerQuery to split data into rows. When exporting data from other systems or sources, it is common to encounter situations where the data is stored in cells combining multiple values. With PowerQuery, we can easily split such data into rows, making the data easier to process and analyze. This can happen if the user doesn't understand Excel's rules and accidentally enters multiple data into a cell, or if the data is not formatted correctly when copying/pasting it from other sources. Processing this data requires additional steps to extract and organize the information for analysis or reporting. How to split data in PowerQuery? PowerQuery transformations can be based on a variety of different factors such as word

Watch4pro and gt each have different features and applicable scenarios. If you focus on comprehensive functions, high performance and stylish appearance, and are willing to bear a higher price, then Watch 4 Pro may be more suitable. If you don’t have high functional requirements and pay more attention to battery life and reasonable price, then the GT series may be more suitable. The final choice should be decided based on personal needs, budget and preferences. It is recommended to carefully consider your own needs before purchasing and refer to the reviews and comparisons of various products to make a more informed choice.
