$ wpdb-> get_row()只返回一行?
4 个回答
- 投票数
-
- 2011-04-08
实际上,仅在希望获得一个结果时才使用
get_row()
,否则可以使用get_results()
Indeed, use
get_row()
only when you expect to get one result, else you can useget_results()
-
供参考,请参阅 https://developer.wordpress.org/reference/classes/wpdb/get_row/ 和 https://developer.wordpress.org/reference/classes/wpdb/get_results/For reference see https://developer.wordpress.org/reference/classes/wpdb/get_row/ and https://developer.wordpress.org/reference/classes/wpdb/get_results/
- 2
- 2017-06-21
- user51928
-
- 2011-04-08
有三种方法可以从数据库中提取数据.
1.
$wpdb->get_var
:使用它从数据库表中获取单个值.就像您要计算评论总数一样.您可以按照以下方式进行操作:<?php $comment_count = $wpdb->get_var($wpdb->prepare("SELECT COUNT(*) FROM $wpdb->comments;")); echo '<p>Total comments: ' . $comment_count . '</p>'; ?>
2.
$wpdb->get_row
:要检索整个表行,可以使用它.示例:
<?php $thepost = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->posts WHERE ID = 1" ) ); echo $thepost->post_title; ?>
OR
<?php $thepost = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->posts WHERE ID = 1" ), ARRAY_A ); print_r ($thepost); ?>
通过在get_row中使用
ARRAY_A
参数,您的帖子数据将作为关联数组返回.另外,您可以使用ARRAY_N
参数以数字索引数组返回您的帖子数据.3.
$wpdb->get_results
:标准SELECT
查询应使用get_results函数从数据库中检索多行数据.<?php global $wpdb; $allposts = $wpdb->get_results( $wpdb->prepare("SELECT ID, post_title FROM $wpdb->posts WHERE post_status = 'publish'") ); foreach ($allposts as $singlepost) { echo '<p>' .$singlepost->post_title. '</p>'; } ?>
,并且您需要最后一个.
There are three ways to pull data from the database.
1.
$wpdb->get_var
:use this to get a single value from the database table. Like if you want to count the total number of comments. You can do it in following way:<?php $comment_count = $wpdb->get_var($wpdb->prepare("SELECT COUNT(*) FROM $wpdb->comments;")); echo '<p>Total comments: ' . $comment_count . '</p>'; ?>
2.
$wpdb->get_row
: To retrieve an entire table row you can use this.Example:
<?php $thepost = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->posts WHERE ID = 1" ) ); echo $thepost->post_title; ?>
OR
<?php $thepost = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->posts WHERE ID = 1" ), ARRAY_A ); print_r ($thepost); ?>
By using the
ARRAY_A
parameter in get_row your post data is returned as an associative array. Alternatively, you could use theARRAY_N
parameter to return your post data in a numerically indexed array.3.
$wpdb->get_results
:StandardSELECT
queries should use the get_results function for retrieving multiple rows of data from the database.<?php global $wpdb; $allposts = $wpdb->get_results( $wpdb->prepare("SELECT ID, post_title FROM $wpdb->posts WHERE post_status = 'publish'") ); foreach ($allposts as $singlepost) { echo '<p>' .$singlepost->post_title. '</p>'; } ?>
and you need the last one, as you can expect.
-
精彩的细节示例.Wonderful detail examples..
- 1
- 2013-01-27
- pixelngrain
-
当然!为什么不..Sure! why not..
- 0
- 2013-01-28
- pixelngrain
-
- 2011-09-01
$wpdb->get_row('query', output_type, row_offset);
行偏移 (整数)所需的行(0为第一行).默认为0.
$wpdb->get_row('query', output_type, row_offset);
row_offset (integer) The desired row (0 being the first). Defaults to 0.
-
- 2016-11-16
我的解决方法很简单.
<?php function count_results() { # use the data base global $wpdb; # Query to count all results from one table $sql_count_results = ' SELECT count(*) as count FROM `YOUR_TABLE`;'; # Ejecute function $results = $wpdb->get_row( $sql_count_results , OBJECT ); # Return results return $results->count; }
使用:
<?php echo count_results();
my solution is simple..
<?php function count_results() { # use the data base global $wpdb; # Query to count all results from one table $sql_count_results = ' SELECT count(*) as count FROM `YOUR_TABLE`;'; # Ejecute function $results = $wpdb->get_row( $sql_count_results , OBJECT ); # Return results return $results->count; }
Use:
<?php echo count_results();
为什么?我在控制台中尝试了相同的查询,它返回了多行.这是查询:
$this->wpdb->get_row("SELECT * FROM ".$this->wpdb->users." WHERE status = 'active'", ARRAY_A);
当有多个活动用户时,它会继续返回同一行.我想念什么吗?