dpowt82802 2015-08-27 15:29
浏览 442
已采纳

将查询结果转换为二维数组

I want to convert my query result to two dimensional array the query is

SELECT c.name,c.mobile_number,t.service_time 
FROM CUSTOMER AS c
JOIN TRANSACTION AS t ON c.id = t.customer_id
WHERE t.service_date = '$date' 
AND employee_id = '$employee_id

I get name(varchar),mobile num(int) and time which is time format. I wrote some code but it is not working:

$results = array();
echo $num_fields=mysqli_num_fields($array1);
echo $num_rows=mysqli_num_rows($array1);
while($line = mysqli_fetch_array($array1))
{
    for($i=0;$i<$num_rows;$i++)
    {
        for($j=0;$j<$num_fields;$j++)
        {
            $results[$i][$j]=$line[$i][$j];
        }
        }
}

The result I am getting is:

[["k ","a ","r "],["9 ","8 ","7 "],["0 ","2 ",": "]]

The output contains only first characters that to one. I want each row of two dimensional array to have row of my query result.

  • 写回答

3条回答 默认 最新

  • dqj5046 2015-08-27 15:37
    关注

    You can just add the result to a new array and change mysqli_fetch_array to mysqli_fetch_row:

    $results = array();
    
    while ($line = mysqli_fetch_row($array1))
    {
      $results[] = $line;
    }
    

    Alternatively you can use mysqli_fetch_assoc() to make for an associative array (e.g. $results[0]['name'] will be the name column of the first row).

    Edit

    If there are duplicate column names you can use aliases:

    SELECT one.name AS one_name, two.name AS two_name FROM one INNER JOIN two USING ...

    The associative array will then have $result[0]['one_name'] and $result[0]['two_name'].

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 Pwm双极模式H桥驱动控制电机
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题