-1
我正在运行一个SQL查询,它返回18个结果,然后我试图破解它们。当我在mysqli_fetch_array
之后立即使用implode命令来破坏mysqli_fetch_array
结果时,我只获得数组的第一个元素。然而,当我在while循环中做同样的事情时,它工作正常。如何内爆mysql查询结果数组
这里是我的代码:(它简化为便于理解)
$q = "SELECT id from sr WHERE m_id = '4';";
$my_query = mysqli_query($conn, $q);
$records = mysqli_num_rows($my_query);
echo 'records=' . $records;
$result = mysqli_fetch_array($my_query, MYSQLI_ASSOC);
print_r ($result);
$imploded_result = implode(",", $result);
echo 'imploded_result=' . $imploded_result;
这里是我所得到的:
records=14
Array ([id] => 14)
imploded_result=14
现在,当我使用while循环,而不是破灭( ),我得到正确的结果:
$imploded_result = '';
while ($result = mysqli_fetch_array($my_query, MYSQLI_ASSOC))
$imploded_result .= $result['id'] . ',';
echo 'imploded_result=' . $imploded_result;
我得到:
imploded_result=14,21,24,27,28,30,31,32,37,152,155,154,156,157,158,159,181,196,
预先感谢您!
'mysqli_fetch_array'只从结果集中提取一条记录。因此,将每个id放入循环中的数组中,并在之后进行分解。 – CBroe
或使用'mysqli_fetch_all'。 – CBroe