Home>Article>CMS Tutorial> 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!