Ahmed Kaicer
Ahmed Kaicer

Reputation: 7

How to make script to execute sql query every 3 seconds for indefinite time UBUNTU 18.04

I want to write php script to execute sql query every 3 seconds automatically and indefinitely until I stop it. this is my sql query that I want to execute

UPDATE users SET bouquet =
  '["12","10","11","8","9","6","7","5","4","3","2","1"]'

The database name is xtream_iptvpro

Sorry, I am a beginner so I really don't know any thing. Thanks for your help

Upvotes: 0

Views: 739

Answers (2)

The Impaler
The Impaler

Reputation: 48770

If you periodically "patch" some column of a table, then the columns will have the wrong data for some time. You probably don't want that.

Instead, I think a trigger will be better, since it will "fix" the data on the fly while being inserted/updated. The table won't ever have the wrong data to begin with.

See 24.3.1 Trigger Syntax and Examples.

For example (tweak as necessary):

create table users (id int, bouquet varchar(100));

create trigger fix_data before insert on users 
for each row 
begin
  set NEW.bouquet = '["12","10","11","8","9","6","7","5","4","3","2","1"]';
end;

Then, if you run the following INSERT statements:

insert into users (id, bouquet) values (1, 'Hello');
insert into users (id) values (2);
insert into users (id, bouquet) values (3, null);
insert into users (id, bouquet) values (4,
  '["12","10","11","8","9","6","7","5","4","3","2","1"]');

select * from users;

You get the result:

id  bouquet                                           
--  ----------------------------------------------------
1   ["12","10","11","8","9","6","7","5","4","3","2","1"]
2   ["12","10","11","8","9","6","7","5","4","3","2","1"]
3   ["12","10","11","8","9","6","7","5","4","3","2","1"]
4   ["12","10","11","8","9","6","7","5","4","3","2","1"]

Upvotes: 1

ieatbytes
ieatbytes

Reputation: 516

you have to use any cli or a some other technology to proceed.

for example if it's mysql you can use PHP, java or you can build it using console command.

while true; do echo -n "execute your query here"; sleep 3; done

while true; do mysql -u root -p somedb -e "select * from mytable"; sleep 3; done

This solultion might help you

Upvotes: 1

Related Questions