YoungNate_Black_coder Posted July 18, 2011 Share Posted July 18, 2011 i want to kno if im going about this corectly i want to update a some database values and delete some values apon a certain time and this cod sits on a cron job runiing every5 mins tell me if ive done anything wrong? <? // grab all clutches and check if they are due yet $con = mysql_connect('localhost','xxxuser','xxxpass'); mysql_select_db('ab6691_snakesbay_game',$con); $all = "SELECT * FROM cluth"; $clu = mysql_query($all)or die(mysql_error()); $all = mysql_fetch_assoc($clu)or die(mysql_error()); // check if they hatched yet while($all = mysql_fetch_assoc($clu)){ $now = strtotime(date("Y-m-d h:i:s")); $due = strtotime($all['due_date']); $id = $all['id']; echo "clutch id : $id"; if($now >= $due){ $all_s = "SELECT * FROM snakes WHERE `clutch_id` = '$id' "; $al_s = mysql_query($all_s); while($snake = mysql_fetch_assoc($al_s)){ // change for everyone! $sid = $snake['snake_id']; $sql = "UPDATE `ab6691_snakesbay_game`.`snakes` SET `staus` = 'nu' WHERE `snakes`.`snake_id` = '$sid'"; mysql_query($sql)or die(mysql_error()); echo "Snakes Have been Updated...<br>"; } $sql = "DELETE FROM `ab6691_snakesbay_game`.`cluth` WHERE `clutch_id` = '$id' "; mysql_query($sql)or die(mysql_error()); }else{ echo "the clutch woth the id <u>$id</u> Does not need to be updated "; } // delete that clutch $del = "DELETE FROM `ab6691_snakesbay_game`.`cluth` WHERE `cluth`.`id` = $id"; mysql_query($del)or die(mysql_error()); } echo "</table>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/242272-update-some-mysql-values-and-delete-some-at-the-same-time-via-cron-job/ Share on other sites More sharing options...
premiso Posted July 18, 2011 Share Posted July 18, 2011 Posting your database credentials is probably not the best idea. If you can still edit it, I would. If you cannot, well you may want to change them on your server. Quote Link to comment https://forums.phpfreaks.com/topic/242272-update-some-mysql-values-and-delete-some-at-the-same-time-via-cron-job/#findComment-1244295 Share on other sites More sharing options...
Psycho Posted July 18, 2011 Share Posted July 18, 2011 ...tell me if ive done anything wrong? Is the code doing what you expect it to? Are there errors? Or, are you just wanting to make sure it has a good design? Quote Link to comment https://forums.phpfreaks.com/topic/242272-update-some-mysql-values-and-delete-some-at-the-same-time-via-cron-job/#findComment-1244299 Share on other sites More sharing options...
YoungNate_Black_coder Posted July 18, 2011 Author Share Posted July 18, 2011 all of the above becuase i was just getting an error before with the die but i removed it and it works fine now , i figured it out own my own with some debugging but is this a good way of going about this because im feeling like my hosting sucks because its takes forever for theese scripts to get executed..... Quote Link to comment https://forums.phpfreaks.com/topic/242272-update-some-mysql-values-and-delete-some-at-the-same-time-via-cron-job/#findComment-1244301 Share on other sites More sharing options...
Psycho Posted July 18, 2011 Share Posted July 18, 2011 all of the above becuase i was just getting an error before with the die but i removed it and it works fine now , i figured it out own my own with some debugging but is this a good way of going about this because im feeling like my hosting sucks because its takes forever for theese scripts to get executed..... Because you are running queries in loop, within loops, etc. That is terribly inefficient. You need to learn how to do JOINs. But, based upon what it looks like you are doing (and a previous thread of yours) I don't think you even need to be running these updates. You can determine what you need by JOINing the tables. I explained all this in the other thread. Also, you are skipping the very first record every time you run that script. Quote Link to comment https://forums.phpfreaks.com/topic/242272-update-some-mysql-values-and-delete-some-at-the-same-time-via-cron-job/#findComment-1244305 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.