katarra Posted March 27, 2010 Share Posted March 27, 2010 I'm trying to write a script (cronjob) that will fluctuate the prices of all the items at a random level (+/- .03% or so), but when I ran this, it changed all the prices to match row #1 and wiped out everything else. (in other words, everything is now the same price as row #1) Thanks for any help! $query = $db->execute("select `price` from `items`"); $item = $query->fetchrow(); $price1 = ceil($item['price']*1.1); $price2 = floor($item['price']*.9); $newprice = mt_rand($price1, $price2); $q = $db->execute("update `items` SET `price`=$newprice"); Quote Link to comment https://forums.phpfreaks.com/topic/196678-fluctuation-of-prices/ Share on other sites More sharing options...
DavidAM Posted March 27, 2010 Share Posted March 27, 2010 Hopefully, there is some unique ID for each row of the table. You need to include that ID in the SELECT statement, loop through each row that is returned, and add a WHERE clause to the UPDATE. Your current UPDATE has no WHERE clause so it is updating every row in the table with the same price. Quote Link to comment https://forums.phpfreaks.com/topic/196678-fluctuation-of-prices/#findComment-1032619 Share on other sites More sharing options...
katarra Posted March 27, 2010 Author Share Posted March 27, 2010 Each has a unique ID, yeah. So, how would I include that in? WHERE `id`=?" to the end of it? I now have (just the rows that changed): $query = $db->execute("select `id`, `price` from `items`"); $q = $db->execute("update `items`, `id` SET `price`=$newprice" ); Quote Link to comment https://forums.phpfreaks.com/topic/196678-fluctuation-of-prices/#findComment-1032620 Share on other sites More sharing options...
andrewgauger Posted March 27, 2010 Share Posted March 27, 2010 $query=$db->execute("select 'id','price' from 'items'"); $id=$item["id"]; $q=$db->execute("update 'items' SET 'price'=$newprice WHERE 'id'=$id"); Quote Link to comment https://forums.phpfreaks.com/topic/196678-fluctuation-of-prices/#findComment-1032630 Share on other sites More sharing options...
katarra Posted March 27, 2010 Author Share Posted March 27, 2010 Hmm... close. It'll change the 1st row, but it doesn't change any of the following rows. $query=$db->execute("select `id`,`price` from `items`"); $item = $query->fetchrow(); $id=$item["id"]; $price1 = ceil($item['price']*1.03); $price2 = floor($item['price']*.97); $newprice = mt_rand($price1, $price2); $q=$db->execute("update `items` SET `price`=$newprice WHERE `id`=$id"); Quote Link to comment https://forums.phpfreaks.com/topic/196678-fluctuation-of-prices/#findComment-1032698 Share on other sites More sharing options...
andrewgauger Posted March 28, 2010 Share Posted March 28, 2010 $query=$db->execute("select `id`,`price` from `items`"); while($item = $query->fetchrow()) { $id=$item["id"]; $price1 = ceil($item['price']*1.03); $price2 = floor($item['price']*.97); $newprice = mt_rand($price1, $price2); $q=$db->execute("update `items` SET `price`=$newprice WHERE `id`=$id"); } Quote Link to comment https://forums.phpfreaks.com/topic/196678-fluctuation-of-prices/#findComment-1032881 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.