Jump to content

obsessed with removing query from loop need help


Ujj

Recommended Posts

Hi again, :D

 

being obsessed with removing queries from loop to optimize my code as suggested, here is my a situation i am stuck while trying to avoid query inside loop

 

this is my t_item table

table1.png

 

and this is item_detail

table2.png

 

now i am running query like this to find out the lowest priced item from each track_item row of table t_item of each user_item of table t_item.

 

after that i also need to find out price for each user_item of t_item table..

 

 


$sqlAdjustPrice = "select * from t_item where `user_id` = 63";
$rst=mysql_query($sqlAdjustPrice);
while($row1=mysql_fetch_array($rst))

{
	$myItem = $row1['user_item'];	
	$trackedItem = $row1['track_item'];
	$adjustPrice = $row1['adjust_price'];
	$groupID = $row1['g_id'];


if($adjustPrice=='y')	{

$sql = "SELECT  price FROM item_detail WHERE itemID in ($trackedItem)  AND groupID='6' ORDER BY price asc LIMIT 1";	
$rs=mysql_query($sql);
$row = mysql_fetch_array($rs);
$lowestPricedTrackedItem = round($row['price'],2);	

	 $sql = "SELECT price FROM item_detail  WHERE `itemID` = '".$myItem."' and `user_id`='63' and groupID='6'";
      $rs=mysql_query($sql);
	$row = mysql_fetch_array($rs);
	$myItemPrice = $row['price'];


}

 

how can i avoid loop here please ?

Just in case it takes 3 people to say it:  You are doing this wrong.  You cannot store comma-delimited lists in a single column.  Why not?  Because of this thread.  It causes this problem.  This problem cannot be solved.  Your database is not designed right.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.