[英]Store query result into php array to echo result as per index
I have below code which queries result from 2 Mysql columns. 我有下面的代码,查询的结果来自2个Mysql列。 I want to store them in a single array.
我想将它们存储在单个数组中。 I went thru many questions and answers and tried a lot and managed below code:
我经过很多问题和解答,并尝试了很多,并在以下代码中进行管理:
$query10 = $conn->query("SELECT CONCAT(step1, ' ', step2) AS A1 FROM workflow1 where nx_version='NX11IP33'");
$array10 = Array();
while( $row = mysqli_fetch_assoc( $query10)){
$new_array[] = $row; // Inside while loop
}
print_r($new_array);
Current output: 电流输出:
Array ( [0] => Array ( [A1] => 10 2 ) )
Desired output: 所需的输出:
Array ( [0] => 10 [1] => 2 )
Any help. 任何帮助。
You specifically CONCAT()
both columns to a single in your sql query. 您在SQL查询中将
CONCAT()
两列专门设置为一个。 And then you ask why they are concatenated instead of being separate? 然后您问为什么将它们串联而不是分开?
The answer then obviously is to fetch both columns separately: 那么显然答案是分别获取两列:
<?php
//...
$query = $conn->query("SELECT step1, step2 FROM workflow1 WHERE nx_version='NX11IP33'");
if ($query) {
$result = [];
while($row = mysqli_fetch_assoc($query)) {
$result[] = $row;
}
print_r($result);
}
The output of that will be something like this: 该输出将是这样的:
Array (
[0] => Array (
[step1] => 10
[step2] => 2
)
)
If nx_version
is a unique key, so if it is guaranteed that you will receive only a single row in the query result, then you can simplify that: 如果
nx_version
是唯一键,那么如果可以保证在查询结果中仅收到一行,则可以简化以下操作:
<?php
//...
$query = $conn->query("SELECT step1, step2 FROM workflow1 WHERE nx_version='NX11IP33' LIMIT 1");
if ($query) {
$result = mysqli_fetch_assoc($query);
print_r($result);
}
The output of that will be something like this: 该输出将是这样的:
Array (
[step1] => 10
[step2] => 2
)
Finally, if you prefer numeric indexes instead of associative ones (as your desired result in the question suggests), then use mysqli_fetch_row()
instead of mysqli_fetch_assoc()
. 最后,如果您更喜欢数字索引而不是关联索引(如问题中的预期结果所示),请使用
mysqli_fetch_row()
代替mysqli_fetch_assoc()
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.