burtybob Posted September 5, 2007 Share Posted September 5, 2007 I have a code that should update a table named drugs after 2 hours however when i try the code i get the data from the database but it then goes into minus plus can some one look at my code and tell me why it goes minus instead? <?php $con = mysql_connect("sql address","username","password"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("database", $con); $londonpdp=mt_rand(99,999); $oslopdp=mt_rand(98,999); $cairopdp=mt_rand(99,999); $shanghaipdp=mt_rand(99,999); $parispdp=mt_rand(99,999); $dublinpdp=mt_rand(99,999); $riopdp=mt_rand(99,999); $stockholmpdp=mt_rand(99,999); $athenspdp=mt_rand(99,999); $montrealpdp=mt_rand(99,999); $washingtonpdp==mt_rand(99,999); $time1=mysql_query("SELECT pricetime FROM drugs"); $outtime=time()+(24*60*60); //24hours $time4= $time1-time(); if ($time4<=0) { mysql_query("UPDATE drugs SET londonprice = '$londonpdp' WHERE drugname = 'pot'"); mysql_query("UPDATE drugs SET osloprice = '$oslopdp' WHERE drugname = 'pot'"); mysql_query("UPDATE drugs SET cairoprice = '$cairopdp' WHERE drugname = 'pot'"); mysql_query("UPDATE drugs SET shanghaiprice = '$shanghaipdp' WHERE drugname = 'pot'"); mysql_query("UPDATE drugs SET washingtonprice = '$washingtonpdp' WHERE drugname = 'pot'"); mysql_query("UPDATE drugs SET dublinprice = '$dublinpdp' WHERE drugname = 'pot'"); mysql_query("UPDATE drugs SET parisprice = '$parispdp' WHERE drugname = 'pot'"); mysql_query("UPDATE drugs SET rioprice = '$riopdp' WHERE drugname = 'pot'"); mysql_query("UPDATE drugs SET stockholmprice = '$stockholmpdp' WHERE drugname = 'pot'"); mysql_query("UPDATE drugs SET athensprice = '$athenspdp' WHERE drugname = 'pot'"); mysql_query("UPDATE drugs SET montrealprice = '$montrealpdp' WHERE drugname = 'pot'"); mysql_query("UPDATE drugs SET pricetime = '$outtime' WHERE drugname = 'pot'"); } mysql_close($con); echo "$outtime <br>"; echo "Time 4: $time4 <br>"; ?> d Quote Link to comment https://forums.phpfreaks.com/topic/68087-solved-randomiseing-and-such-likes/ Share on other sites More sharing options...
Daniel0 Posted September 5, 2007 Share Posted September 5, 2007 Try this <?php $con = mysql_connect("sql address","username","password") or die('Could not connect: '.mysql_error()); mysql_select_db("database", $con); $prices = array(); foreach(array('london', 'oslo', 'cairo', 'shanghai', 'paris', 'dublin', 'rio', 'stockholm', 'athens', 'montreal', 'washington') as $city) { $prices[$city] = mt_rand(99, 999); } list($time1) = mysql_fetch_array(mysql_query("SELECT pricetime FROM drugs")); $outtime = time()+(24*60*60); //24hours $time4= $time1-time(); if($time4<=0) { $sets = array(); foreach($prices as $city => $price) { $sets[] = "{$city}price='{$price}'"; } mysql_query("UPDATE drugs SET ".join(',',$sets)." WHERE drugname = 'pot'"); } mysql_close($con); echo "$outtime <br>"; echo "Time 4: $time4 <br>"; ?> You might want to reconsider your database layout. It's better to store each city as an entry for itself rather than having a lot of fields. Quote Link to comment https://forums.phpfreaks.com/topic/68087-solved-randomiseing-and-such-likes/#findComment-342234 Share on other sites More sharing options...
burtybob Posted September 5, 2007 Author Share Posted September 5, 2007 That code still gives me a minus and doesnt update the db unless i rtefresh the page, my idea is that it updates it every so often my i should use on of my other scripts and edit that, thank you for your advice tho. Quote Link to comment https://forums.phpfreaks.com/topic/68087-solved-randomiseing-and-such-likes/#findComment-342239 Share on other sites More sharing options...
Daniel0 Posted September 5, 2007 Share Posted September 5, 2007 What goes into "minus"? The prices? Quote Link to comment https://forums.phpfreaks.com/topic/68087-solved-randomiseing-and-such-likes/#findComment-342244 Share on other sites More sharing options...
burtybob Posted September 5, 2007 Author Share Posted September 5, 2007 What goes into "minus"? The prices? No the prices are fine but the time goes into minus when i go on the page the time from the DB - Current time for some reason seem to go into minuse. 1189102314 <---- Time now Time 4: -1189015805 <------ Time in DB - Time now So because it never hits 0 it never automatically updates the DB. Quote Link to comment https://forums.phpfreaks.com/topic/68087-solved-randomiseing-and-such-likes/#findComment-342246 Share on other sites More sharing options...
Daniel0 Posted September 5, 2007 Share Posted September 5, 2007 Try to echo $time1 and see what it says to check if you get the time correctly. What is the time in $time1 supposed to be? The time of the last update? What is $outtime supposed to be used for? Quote Link to comment https://forums.phpfreaks.com/topic/68087-solved-randomiseing-and-such-likes/#findComment-342248 Share on other sites More sharing options...
burtybob Posted September 5, 2007 Author Share Posted September 5, 2007 Thanks one of my mates that helps to code, has just realised that the time in the db that it is getting is the first one on the list which is why it is totally messing so thank you for your help, even though it idnt work it has tidied my code up a lot. Quote Link to comment https://forums.phpfreaks.com/topic/68087-solved-randomiseing-and-such-likes/#findComment-342256 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.