dongzhao5834 2015-10-13 21:19
浏览 108

PHP Mysqli从JOIN返回数组(别名为数组键)

Ok so I have this query, which works quite good, but it doesn't return what i want to.

SELECT acc.*, fin.* 
FROM consumers acc
LEFT JOIN finances fin ON fin.uid = acc.uid 
WHERE acc.id = '$userID';

This returns an array (in php) where the key is 0 and all the columns (from both tables) are under the same array.

What I want to achieve is get 2 arrays, 1 with columns from first table, and another 1 with columnds from the second table.

Basically i'm trying to get a multidimensional array out of 1 query.

  • 写回答

2条回答 默认 最新

  • doulong2782 2015-10-13 21:40
    关注

    Split it into two arrays after reading the row.

    $row = mysqli_fetch_assoc($query);
    $acc = array($row['col1'], $row['col2'], ...);
    $fin = array($row['col6'], $row['col7'], ...);
    

    where col1,col2, ... are the columns fromconsumer, andcol6,col7, ... are the columns from$finances`.

    A way to do it without listing all the column names is with array_slice()

    $row = mysqli_fetch_assoc($query);
    $acc = array_slice($row, 0, $number_of_consumer_columns);
    $fin = array_slice($row, $number_of_consumer_columns, $number_of_finances_columns);
    $table3 = array_slice($row, $number_of_consumer_columns + $number_of_finances_columns, $number_of_table3_columns);
    

    You can continue this for all the tables. It would be straightforward to put this into a function that takes an array of all the column counts, and returns a 2-dimensional array containing each table's data.

    评论

报告相同问题?

悬赏问题

  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥15 可见光定位matlab仿真