Home > CMS Tutorial > WordPress > body text

How to get data from database in wordpress

爱喝马黛茶的安东尼
Release: 2019-07-12 15:37:28
Original
5563 people have browsed it

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");
Copy after login

For example:

$var = $wpdb -> get_var("SELECT count(*) FROM `user`");
Copy after login

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);
Copy after login

For example:

$var = $wpdb -> get_row("SELECT * FROM `user` WHERE `userid` = 1", ARRAY_A);
Copy after login

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");
Copy after login

For example:

$var = $wpdb -> get_col("SELECT `age` FROM `user`);
Copy after login

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);
Copy after login

For example:

$vars = $wpdb -> get_results("SELECT * FROM `user`, ARRAY_A);
Copy after login

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时
}
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!