sql - 将 SQL 查询输出为数组

标签 sql arrays wordpress

我正在使用以下命令从 WordPress 数据库中的表中检索记录...

global $wpdb;
echo '<table>';
$sellers = get_users('blog_id=1&orderby=nicename&role=sellers');
foreach ($sellers as $seller) 
    {
    echo '<tr>';
    echo '<td>' . $seller->user_login . '</td>';

    $count1 = $wpdb->get_results("SELECT COUNT(*) AS count FROM wp_mymeta2 a JOIN wp_mymeta1 b ON b.id = a.my_id WHERE a.value = '$seller->user_login' AND b.date_posted LIKE '%2014-04-19%'" );
    echo '<td>' . $count1[0]->count . '</td>'; 

    $count2 = $wpdb->get_results("SELECT COUNT(*) AS count FROM wp_mymeta2 a JOIN wp_mymeta1 b ON b.id = a.my_id WHERE a.value = '$seller->user_login' AND b.date_posted LIKE '%$2014-04-18%'" );
    echo '<td>' . $count2[0]->count . '</td>';

    $count3 = $wpdb->get_results("SELECT COUNT(*) AS count FROM wp_mymeta2 a JOIN wp_mymeta1 b ON b.id = a.my_id WHERE a.value = '$seller->user_login' AND b.date_posted LIKE '%2014-05-17%'" );
    echo '<td>' . $count3[0]->count . '</td>'; 

    echo '</tr>';
    }
echo '</table>';

这非常有效,并且返回我想要的数据并将其回显到表格中。

我现在想将数据放入数组中,这样我最终会得到......

var $example_data = array(
            array(
                'seller'    => '$seller',
            'date'      => '$date1',
                'count'     => '$count_result',
            ),
        array(
                'seller'    => '$seller',
            'date'      => '$date2',
                'count'     => '$count_result',
            ),
        array(
                'seller'    => '$seller',
            'date'      => '$date3',
                'count'     => '$count_result',
            ),
        );

有没有人有一个例子可以为我指明实现类似目标的正确方向?

最佳答案

您可以在 foreach 循环中使用 array_push 函数

global $wpdb;
echo '<table>';
$sellers = get_users('blog_id=1&orderby=nicename&role=sellers');
$arrSellers = array();

foreach ($sellers as $seller) 
{
echo '<tr>';
echo '<td>' . $seller->user_login . '</td>';

$count1 = $wpdb->get_results("SELECT COUNT(*) AS count FROM wp_mymeta2 a JOIN wp_mymeta1 b ON b.id = a.my_id WHERE a.value = '$seller->user_login' AND b.date_posted LIKE '%2014-04-19%'" );
echo '<td>' . $count1[0]->count . '</td>'; 

$count2 = $wpdb->get_results("SELECT COUNT(*) AS count FROM wp_mymeta2 a JOIN wp_mymeta1 b ON b.id = a.my_id WHERE a.value = '$seller->user_login' AND b.date_posted LIKE '%$2014-04-18%'" );
echo '<td>' . $count2[0]->count . '</td>';

$count3 = $wpdb->get_results("SELECT COUNT(*) AS count FROM wp_mymeta2 a JOIN wp_mymeta1 b ON b.id = a.my_id WHERE a.value = '$seller->user_login' AND b.date_posted LIKE '%2014-05-17%'" );
echo '<td>' . $count3[0]->count . '</td>'; 

echo '</tr>';

 //add whatever you want to $arrSellers using array_push

}
echo '</table>';

关于sql - 将 SQL 查询输出为数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23174817/

相关文章:

arrays - 使用嵌套元组对数组进行排序

php - 如何在服务器目录联系表 7 上保留上传的文件或附件

php - 从 mysql 数据库中提取序列化数据并在 php 中反序列化和使用各个字段

sql - Postgres 警告 : pg_query(): Query failed: ERROR: operator does not exist: boolean = integer LINE 1

sql - 使用其他表中的多个值的随机更新表

mysql - mysql中的复杂查询用于按日期生成报告

c# - 当特定字段为空时如何获取行数

JavaScript 从数组中查找值,替换为下一个索引

php 数组只有一种类型

php - 用PHP从Mysql数据库获取数据报错