How to get data from database in wordpress
1. Obtain single data, used when general SQL statements only return one value.
$var = $wpdb -> get_var("query");
For example:
$var = $wpdb -> get_var("SELECT count(*) FROM `user`");
This function returns the value directly and can be used directly.
Note: In fact, get_var is not just a SQL statement that can only return a value, but it only returns the leftmost element of the first row by default. If you want it to return other elements, you can use get_var("query", x, y).
2. Get a row of data, used when general SQL statements only return specific objects.
$sql = $wpdb -> get_row("query", output_type);
For example:
$var = $wpdb -> get_row("SELECT * FROM `user` WHERE `userid` = 1", ARRAY_A);
output_type: One of three predefined constants. The default value is OBJECT.
OBJECT - The returned result is output in the form of an object
ARRAY_A - The returned result is output in the form of an associative array
ARRAY_N - The returned result is output in the form of a numerical index array Output
I usually use OBJECT or ARRAY_A, and the access method is $var -> username (when output_type is OBJECT) or $var["username"] (when output_type is ARRAY_A)
Note: In fact, get_row is not just a sql statement that can only be used to return one row, but it only returns the first row set by default. If you want it to return other rows, you can use get_row("query", output_type, y).
Related recommendations: "WordPress Tutorial"
3. Get a column of data, used when general SQL statements only return specific attributes.
$sql = $wpdb -> get_col("query");
For example:
$var = $wpdb -> get_col("SELECT `age` FROM `user`);
The returned result is output in the form of a numerical index array, usually separated by the foreach function, or obtained directly using $var[1].
Note: In fact, get_col is not just a SQL statement that can only be used to return one column, but it only returns the first column set by default. If you want it to return other columns, you can use get_col("query", x) to achieve it.
4. Obtain multi-column data, used when general SQL statements only return specific attributes.
$sql = $wpdb -> get_results("query", output_type);
For example:
$vars = $wpdb -> get_results("SELECT * FROM `user`, ARRAY_A);
The returned result is output in the form of a numerical index array and other forms. It is usually separated by the foreach function, or obtained directly using $var[1]. The object obtained is controlled by the second parameter.
output_type: One of three predefined constants. The default value is OBJECT.
OBJECT - The returned result is output in the form of an object
ARRAY_A - The returned result is output in the form of an associative array
ARRAY_N - The returned result is output in the form of a numerical index array Output
I usually use OBJECT or ARRAY_A, and the access method is $var -> username (when output_type is OBJECT) or $var["username"] (when output_type is ARRAY_A).
For example:
foreach($vars as $var) { echo $var["username"];//output_type是ARRAY_A时 }
The above is the detailed content of How to get data from database in wordpress. For more information, please follow other related articles on the PHP Chinese website!

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



PHP and Flutter are popular technologies for mobile development. Flutter excels in cross-platform capabilities, performance and user interface, and is suitable for applications that require high performance, cross-platform and customized UI. PHP is suitable for server-side applications with lower performance and not cross-platform.

Hibernate polymorphic mapping can map inherited classes to the database and provides the following mapping types: joined-subclass: Create a separate table for the subclass, including all columns of the parent class. table-per-class: Create a separate table for subclasses, containing only subclass-specific columns. union-subclass: similar to joined-subclass, but the parent class table unions all subclass columns.

Apple's latest releases of iOS18, iPadOS18 and macOS Sequoia systems have added an important feature to the Photos application, designed to help users easily recover photos and videos lost or damaged due to various reasons. The new feature introduces an album called "Recovered" in the Tools section of the Photos app that will automatically appear when a user has pictures or videos on their device that are not part of their photo library. The emergence of the "Recovered" album provides a solution for photos and videos lost due to database corruption, the camera application not saving to the photo library correctly, or a third-party application managing the photo library. Users only need a few simple steps

WordPress requires registration. According to my country's "Internet Security Management Measures", websites that provide Internet information services within the country must register with the local provincial Internet Information Office, including WordPress. The registration process includes steps such as selecting a service provider, preparing information, submitting an application, reviewing and publishing, and obtaining a registration number. The benefits of filing include legal compliance, improving credibility, meeting access requirements, ensuring normal access, etc. The filing information must be true and valid, and must be updated regularly after filing.

How to use MySQLi to establish a database connection in PHP: Include MySQLi extension (require_once) Create connection function (functionconnect_to_db) Call connection function ($conn=connect_to_db()) Execute query ($result=$conn->query()) Close connection ( $conn->close())

To handle database connection errors in PHP, you can use the following steps: Use mysqli_connect_errno() to obtain the error code. Use mysqli_connect_error() to get the error message. By capturing and logging these error messages, database connection issues can be easily identified and resolved, ensuring the smooth running of your application.

Through the Go standard library database/sql package, you can connect to remote databases such as MySQL, PostgreSQL or SQLite: create a connection string containing database connection information. Use the sql.Open() function to open a database connection. Perform database operations such as SQL queries and insert operations. Use defer to close the database connection to release resources.

Yes, WordPress is a CMS that allows users to manage websites without coding. However, advanced features and customizations may require some coding knowledge, including themes, plugins, widgets, custom post types, and functions. Code level requirements vary depending on the goals of the implementation, typically no code is required for basic functionality, but basics of HTML, CSS, and PHP are required for advanced customization.
