Sinikka Posted October 18, 2007 Share Posted October 18, 2007 Can anyone see what is wrong with this? Instead of randomly choosing what to stock and dividing the max_stock in half it's stocking everything with one being the amount in stock. I've tried messing around with it but I can't figure it out. Any help would be appreciated! <?php #!/usr/bin/php $dbh=mysql_connect ("removed", "removed", "removed") or die ('I cannot connect to the database because: ' . mysql_error()); mysql_select_db ("removed"); $findItems = mysql_query("SELECT * FROM shop_items2 WHERE game = '1'"); while ($getItems = mysql_fetch_array($findItems)) { $id = $getItems[id]; $getItems[max_stock] = round($getItems[max_stock] / 2); $rand[$id] = rand(0,$getItems[max_stock]); } $sql2="UPDATE `shop_items2` SET cur_stock=$rand[$id] WHERE game = '1'"; if(mysql_query($sql2)) { echo $sql2; } ?> Link to comment https://forums.phpfreaks.com/topic/73767-solved-quick-question/ Share on other sites More sharing options...
ToonMariner Posted October 18, 2007 Share Posted October 18, 2007 not really clear what you are doing - perhaps you should print out the results of your query to see what you are actually selecting (is the result just one record or lots? - if its lots then remember that your final query will NOT hold different values for $rand[$id]; that will simply be the last value calculated in your loop!). Link to comment https://forums.phpfreaks.com/topic/73767-solved-quick-question/#findComment-372192 Share on other sites More sharing options...
Sinikka Posted October 18, 2007 Author Share Posted October 18, 2007 The script is for one of my shop restocking crons. I need to to take the max stock possible and divide that in half but then choose the items that it pulls randomly so it's not stocking all items that can possibly stock each time. When I execute the script this is what I get: UPDATE `shop_items2` SET cur_stock=1 WHERE game = '1' *edit and it would stock multiple shops at once so it would be more then one record. Link to comment https://forums.phpfreaks.com/topic/73767-solved-quick-question/#findComment-372194 Share on other sites More sharing options...
Barand Posted October 18, 2007 Share Posted October 18, 2007 your update sets all the cur_stock values to the last value pulled in the previous loop try foreach ($rand as $id => $val) { $sql2="UPDATE `shop_items2` SET cur_stock=$val WHERE id = '$id' "; mysql_query($sql2) } Link to comment https://forums.phpfreaks.com/topic/73767-solved-quick-question/#findComment-372196 Share on other sites More sharing options...
Sinikka Posted October 18, 2007 Author Share Posted October 18, 2007 That gives me: Parse error: syntax error, unexpected '}' in /home/aces12/public_html/restock.php on line 21 Link to comment https://forums.phpfreaks.com/topic/73767-solved-quick-question/#findComment-372200 Share on other sites More sharing options...
ToonMariner Posted October 18, 2007 Share Posted October 18, 2007 missed the ; after mysql_query.... Link to comment https://forums.phpfreaks.com/topic/73767-solved-quick-question/#findComment-372201 Share on other sites More sharing options...
Barand Posted October 18, 2007 Share Posted October 18, 2007 Sorry, missing ; after mysql_query($sql2) Alternatively, replace all your above code with a single update query UPDATE shop_items2 SET cur_stock = ROUND((max_stock/2) * RAND() ) WHERE game = '1' Link to comment https://forums.phpfreaks.com/topic/73767-solved-quick-question/#findComment-372202 Share on other sites More sharing options...
Sinikka Posted October 18, 2007 Author Share Posted October 18, 2007 Thank you so much that worked perfectly Link to comment https://forums.phpfreaks.com/topic/73767-solved-quick-question/#findComment-372203 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.