I'm trying to execute a query inside a WHILE loop running on mysqli_fetch_array. The query gives the following error:
Error: Commands out of sync; you can't run this command now
I found out that you can't have simultaneous queries. Here's my code
$query "SELECT * FROM table";
$exec = mysqli_query($con, $query);
while($loop = mysqli_fetch_array($exec)){
$data = $loop['data'];
$sim = "SELECT * FROM table2 WHERE col1 = '$data'";
$execsim = mysqli_query($con, $sim);
$getsim = mysqli_fetch_array($execsim);
$somedata = $getsim['somedata'];
//insert $somedata and $data into table3
}
After some research, I see that store_result() could be useful here. Excuse my ignorance as this is new to me. There aren't any examples/solutions that I can find that use this with a WHILE loop scenario (does that make a difference?) - most are in object oriented style too.
What is the most effective way around this problem?
SELECT * FROM table2 WHERE col1 IN (SELECT data FROM table)would get all the entries from table2 where col1 matches data. At that point, you'd be able to insert table2.somedata and table2.col1 into table3.SELECT * FROM table2 WHERE col1 IN (SELECT * FROM table WHERE col2 = 'something')then run the WHILE loop from that query?$datawon't contain SQL text.