E
E
Eugene2018-01-30 11:50:39
PHP
Eugene, 2018-01-30 11:50:39

How to transfer the value received from the database to a variable?

Sending a query to the database

$items     = $wpdb->get_results( "SELECT count(*) FROM `avt_posts` WHERE `post_type` = 'shop_order'" );
var_dump($items);

get
array(1) { [0]=> object(stdClass)#21691 (1) { ["count(*)"]=> string(3) "145" } }

But I can't figure out how to transfer the value "145" to a variable. Tell me how to do it?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
D
Dmitry Kim, 2018-01-30
@Zoten

$items = $wpdb->get_results( "SELECT count(*) AS `count` FROM `avt_posts` WHERE `post_type` = 'shop_order'" ); // изменённый запрос
$item = reset($items);
$x = $item->count;

C
cybernoob, 2018-01-30
@cybernoob

$var = $items[0]['count(*)'];

A
Alexander, 2018-01-30
@SAnhPa

$count = $wpdb->get_var( "SELECT count(*) FROM `avt_posts` WHERE `post_type` = 'shop_order'" );
echo $count;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question