Thank you for your attention. I am newbie in php & MySQL and have one project as simple flow. Here I have one question and would like to know the exact things. So when add data into db with foreach in php, what is the best way to do it for performance of code?
here is one example.
first case.
foreach ($data as $key=>$item){//start for each loop $id = $item['id']; $Name = $item['adsname']; if (!$id || !$bingAdsName) { continue; } $sql="select* from table name "; //$dbData = ... //fetch all db data. //And below it will compare dbdata and the above id&Name with some condition and add to db these value. }//end foreach loop
second case
foreach ($data as $key=>$item){ $id = $item['id']; $Name = $item['adsname']; if (!$id || !$bingAdsName) { continue; } $sql="select* from table name where id='".$id."' AND name='".$Name."'"; $existData= ...; if(!existData){ continue; } $sql="INSERT INTO table name(id, name,) VALUES ('".$id."', '".$name."'); "; //here insert id& value into db; }
So in first case, I will call db connection only one,but it will deal with 2 dimensional foreach.
in second case, it will call db connection the count of $data
times.
So I would like to know what is best way to reduce the running time of code for performance. thanks.