ianmc ianmc - 7 months ago 9
PHP Question

Making multiple mysqli queries using a for loop

I'm trying to make a series of mysqli queries by looping through the values of an array - (called column) - but nothing is being returned.

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]";

}


}

Answer

Just replace your given code with following and see result ..........

$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]";

}