我想将查询结果转换为查询为
的二维数组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
我得到name(varchar),mobile num(int)
和时间格式。我写了一些代码,但它不起作用:
$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];
}
}
}
我得到的结果是:
[["k ","a ","r "],["9 ","8 ","7 "],["0 ","2 ",": "]]
输出仅包含第一个字符。我希望每行二维数组都有我的查询结果行。
答案 0 :(得分:1)
您只需将结果添加到新数组并将mysqli_fetch_array
更改为mysqli_fetch_row
:
$results = array();
while ($line = mysqli_fetch_row($array1))
{
$results[] = $line;
}
或者,您可以使用mysqli_fetch_assoc()
制作关联数组(例如$results[0]['name']
将成为第一行的name
列。
修改强>
如果存在重复的列名,则可以使用别名:
SELECT one.name AS one_name, two.name AS two_name FROM one INNER JOIN two USING ...
关联数组将具有$result[0]['one_name']
和$result[0]['two_name']
。
答案 1 :(得分:0)
试试这个:
while($line = mysqli_fetch_row($array1))
{
array_push($result, $line);
}
答案 2 :(得分:0)
这应该这样做
$con = mysqli_connect("localhost","my_user","my_password","my_db");
$result = mysqli_query($con, "SELECT * FROM .....");
$rows = array();
if($result){
while($row = mysqli_fetch_assoc($con, $result)){
array_push($rows, $row);
}
mysqli_free_result($result);
}
var_dump($rows);