Jump to content

Recommended Posts

Hi guys.. first of all my name is Robert im a bit newbie and just learning bit by bit PHP :) it's great to have find this community and really hope and appreciate if someone could help me with the following:

So basically i want to make a cronjob that once every hour updates the value X from the table Z..

The value X i would want it that every hour changes to 1.. and then after 1 more hour changes to 0.. and so on all day long

I would really appreciate if someone can show me what code should i use exactly so that it does this.. i can do the DB connection and set up the cronjob properly but i really dont know what the code should look like to do the updates i said..

 

Thanks and any help is greatly appreciated !

 I hope i gave enough info with what i want to do .. if not please ask i will try to explain better

Thanks !

Link to comment
https://forums.phpfreaks.com/topic/309656-help-with-a-cronjob-sql-auto-update/
Share on other sites

A couple of methods spring to mind

1 ) toggle the value between 0 and 1 ...

UPDATE tableZ
SET x = CASE 
            WHEN x = 1 THEN 0 
            ELSE 1
        END;

   or

UPDATE tableZ
SET x = (x = 0);

 

2 ) set it based on the time

UPDATE tableZ
SET x = HOUR(NOW()) MOD 2;

Of course the queries may need a WHERE clause unless you want to apply the change to every record in tableZ.

Edited by Barand

A ok sir thank you very much.. 1 more quick question i would have if you may please.. i am pretty bad at this sorry :))

So lets say i have the following table Z:

ID         value

1              0

2              0

 

Now..  i tried in my cronjob i did "UPDATE Z SET value=1" i did this set a cronjob for the first 30 minutes of an hour .. then the next 30 minutes i can just add another cronjob that sets the value 0.. ( i found this to be easier i think that using a toggle because i am noob )

So everything is fine.. the above command updates my values.. but i want only for ID 1 to do the value change not all.. how can i do that please ?

What do i need to change or add in the above command to only update the ID 1 ? I tried with a WHERE but i dont think i placed it right it didnt work..

 

Thanks sir

 

Edited by sKoop
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.