使用 for 循环进行多个 mysqli 查询


Making multiple mysqli queries using a for loop

我正在尝试通过循环数组的值来进行一系列mysqli查询 - (称为列) - 但没有返回任何内容。

for ($i = 0; $i < sizeof($column); $i++)
{
    $mutualInterests = mysqli_query ($conn, "SELECT USER_1 FROM INTERESTS WHERE answer = " . $column[$i] );
    while ($row = mysqli_fetch_array ($mutualInterests))
    {
        echo " $row[USER_1]";
    }

}

只需将您给定的代码替换为以下内容并查看结果......

$query='SELECT USER_1 FROM INTERESTS WHERE answer ="'.$column[0].'"';
for ($i = 1; $i < sizeof($column); $i++)
{
$query.=' OR answer ="'.$column[$i].'"';
}
$mutualInterests = mysqli_query ($conn,$query);
while ($row = mysqli_fetch_array ($mutualInterests))
{
    echo " $row[USER_1]";
}

替换这个:

while ($row = mysqli_fetch_array ($mutualInterests))
{
    echo " $row[USER_1]";
}

有了这个:

$count = 0;
while ($row = mysqli_fetch_array ($mutualInterests))
{
    echo $row[$count]."<br>";
    $count++;
}

让我知道它现在是否有效! :)