【问题标题】:echo different column values from different column from mysqli result从 mysqli 结果的不同列中回显不同的列值
【发布时间】:2019-05-22 00:38:35
【问题描述】:

我正在根据我在mysql中添加的计划制作一个计划比较表。

这是查询

SELECT * 
FROM webhosting 
INNER JOIN webhosting_cat
ON webhosting.cat = webhosting_cat.id
WHERE cat='$cat'

例如,“webhosting”表有以下列:plan、price、ssd_space

该 mysql 查询将返回 3 个结果,我希望可以选择打印结果,例如 $row['plan'][0]$row['plan'][1]$row['plan'][2]

这可能吗?

我使用 php 7.3 和 MariaDB。

【问题讨论】:

  • 我并没有完全按照你的想法去做。您最终应该得到类似$row['plan']$row['price']$row['ssd_space'] 的结果(以及webhosting_cat 表中可用的任何列。
  • 非常感谢您的回答。我试图回显不同的每个单独的 mysql 查询的 3 个结果,例如,一列被命名为 plan,但是里面有 3 个结果,planpro1、planpro2 和 planpro3。
  • 我不想使用 while() 来打印每个示例的 $row['plan']
  • 已修复。我用 $rows[$row['plan']] = $row;没多久
  • while($row = $result->fetch_array()) { $rows[] = $row; } 让你做$rows[0]['plan']

标签: php mysqli


【解决方案1】:

由于我不确切知道您将如何使用您的数据,因此重新组合您的数据 PHP 可能是最有益的。

$arr = [[ 'foo' => 1, 'bar' => 1 ], [ 'foo' => 2, 'bar' => 2 ], [ 'foo' => 3, 'bar' => 3 ], [ 'foo' => 4, 'bar' => 4 ], [ 'foo' => 5, 'bar' => 5 ]];
function regroup($arr) {
    return array_reduce($arr, function($acc, $row) {
        foreach($row as $k => $v) $acc[$k][] = $v;
        return $acc;
    }, []);
}
print_r(regroup($arr));

// Array (
//     [foo] => Array
//              (
//             [0] => 1
//             [1] => 2
//             [2] => 3
//             [3] => 4
//             [4] => 5
//         )
//
//     [bar] => Array
//              (
//             [0] => 1
//             [1] => 2
//             [2] => 3
//             [3] => 4
//             [4] => 5
//         )
// )

【讨论】:

    猜你喜欢
    • 2014-10-23
    • 1970-01-01
    • 1970-01-01
    • 2019-06-03
    • 2016-10-03
    • 2023-04-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多