...

View Full Version : PHP Cron job



itsonlybarney
05-04-2006, 07:36 AM
Hi Everyone,

I'm trying to create a cron job that deletes the database tables and then restores them automatically using a cron job. What I want to know is how would I insert the SQL commands to enable that restoration process to occur from the .sql file from phpmyadmin.

I could just use the sql file in phpmyadmin but I want to do it automatically every few hours.

And I have read many many questions similar to mine and the anwers were to use SSH, I don't have access to SSH on my server.

raf
05-04-2006, 08:41 AM
how large is the .sql file?
if it's just the create statements, then you can read it and execute all statements seperately with mysql_query() ...

itsonlybarney
05-04-2006, 10:22 AM
its only 140kb.

Can someone point in the right direction to create a script to read that from the .sql file?

raf
05-04-2006, 10:48 PM
like so


$commands = file_get_contents('create.sql');
$arr_commands = explode(';', $commands);
foreach($arr_commands as $sql){
mysql_query($sql) or die('Queryproblem in query "'. $sql .'"<br />Error='. mysql_error());
}

felgall
05-05-2006, 12:03 AM
That or die is a bit pointless in a CRON job as there is nothing to dispay the message.

itsonlybarney
05-05-2006, 02:24 AM
That or die is a bit pointless in a CRON job as there is nothing to dispay the message.
I can remove the 'displays' after I know the sript works perfectly. But I'll use them during testing.

It works perfectly except for one thing. A few of the INSERT commands have a ";" in it and it always comes up with a problem when it reaches that. Is there a way around it?

raf
05-05-2006, 07:31 AM
It works perfectly except for one thing. A few of the INSERT commands have a ";" in it and it always comes up with a problem when it reaches that. Is there a way around it?
I suppose there will be some regex way to get out of this, but you'll always be able to still cause problems (like for
INSERT INTO rable (foo) VALUES('INSERT INTO table2 (bar) VALUES(1); INSERT INTO table2 (bar) VALUES(2);INSERT INTO table2 (bar) VALUES(3);');

maybe you can use another separator, like a linebreak or so.
we'll need to see the content of the file to suggest the best possible way.

not realy clean, but if you wount be editing this .sql file often, then the easiest sollution is just to replace the ; at the end of each statement by a | or so, and then use the | as seperator.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum