Why I am getting the Error “Commands out of sync; you can't run this command now”

As asked in previous question When i am executing 2 queries in a single go im getting following error message…

Multi query failed: (2014) Commands out of sync; you can’t run this command now

Below is my php code

        $sql=  "INSERT into inverterlog (`id`,`timestamp`,`irradiance`,`ambienttemp`,`photovoltaictemp`,`pv1voltage`,`pv2voltage`,`pv3voltage`,`pv1current`,`pv2current`,`pv3current`,`pv1power`,`pv2power`,`pv3power`,`pv1energy`,`pv2energy`,`pv3energy`,`gridvoltagegv1`,`gridvoltagegv2`,`gridvoltagegv3`,`gridcurrentgc1`,`gridcurrentgc2`,`gridcurrentgc3`,`gridpowergp1`,`gridpowergp2`,`gridpowergp3`,`sumofapparentpower`,`gridpowertotal`,`gridenergyge1`,`gridenergyge2`,`gridenergyge3`,`socounter`,`gridcurrentdcgc1`,`gridcurrentdcgc2`,`gridcurrentdcgc3`,`gridresidualcurrent`,`gridfrequencymean`,`dcbusupper`,`dcbuslower`,`temppower`,`tempaux`,`tempctrl`,`temppower1`,`temppowerboost`,`apparentpowerap1`,`apparentpowerap2`,`apparentpowerap3`,`sovalue`,`reactivepowerrp1`,`reactivepowerrp2`,`reactivepowerrp3`,`opmode`,`latestevent`,`pla`,`reactivepowermode`,`overexcitedunderexcited`,`reactivepowerabs`,`inverter`)
						                                  values('','$newDate','$emapData[1]','$emapData[2]','$emapData[3]','$emapData[4]','$emapData[5]','$emapData[6]','$emapData[7]','$emapData[8]','$emapData[9]','$emapData[10]','$emapData[11]','$emapData[12]','$emapData[13]','$emapData[14]','$emapData[15]','$emapData[16]','$emapData[17]','$emapData[18]','$emapData[19]','$emapData[20]','$emapData[21]','$emapData[22]','$emapData[23]','$emapData[24]','$emapData[25]','$emapData[26]','$emapData[27]','$emapData[28]','$emapData[29]','$emapData[30]','$emapData[31]','$emapData[32]','$emapData[33]','$emapData[34]','$emapData[35]','$emapData[36]','$emapData[37]','$emapData[38]','$emapData[39]','$emapData[40]','$emapData[41]','$emapData[42]','$emapData[43]','$emapData[44]','$emapData[45]','$emapData[46]','$emapData[47]','$emapData[48]','$emapData[49]','$emapData[50]','$emapData[51]','$emapData[52]','$emapData[53]','$emapData[54]','$emapData[55]','$inverter');";
        
		   $sql.= "INSERT into data (`id`,`timestamp`,`gridpowertotal`,`inverter`) values ('','$newDate','$emapData[26]','$inverter');";

          if (!$con->multi_query($sql)) {
            echo "Multi query failed: (" . $con->errno . ") " . $con->error;
                                  echo "<script type=\"text/javascript\">
					alert(\"Record Insertion Failed.\");
					</script>";
                                    }

and i know some of u might suggest to use prepare stmnt…bnce iu get rid of this error ill use prepare stmnt…

As answered in the previous question, stop meddling with multi query.

One of the issues is that when you call mysqli_multi_query(), you have to deal with all the responses from it before you can start using other queries, either as another multi or just as a single query. Have a read of the user-supplied comments on the bottom of the doc page ( http://php.net/manual/en/mysqli.multi-query.php ) and you’ll see exactly the error you are getting, and how to deal with it. And how it’s so much easier to not have to, just by separating out the queries and running them separately.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.