Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7

Thread: PHP Cron job

  1. #1
    Regular Coder
    Join Date
    May 2004
    Location
    Sydney, Australia
    Posts
    281
    Thanks
    0
    Thanked 0 Times in 0 Posts

    PHP Cron job

    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.
    Last edited by itsonlybarney; 05-04-2006 at 08:06 AM.

  • #2
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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() ...
    Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html

  • #3
    Regular Coder
    Join Date
    May 2004
    Location
    Sydney, Australia
    Posts
    281
    Thanks
    0
    Thanked 0 Times in 0 Posts
    its only 140kb.

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

  • #4
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    like so
    PHP Code:
    $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());

    Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html

  • #5
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,479
    Thanks
    0
    Thanked 635 Times in 625 Posts
    That or die is a bit pointless in a CRON job as there is nothing to dispay the message.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #6
    Regular Coder
    Join Date
    May 2004
    Location
    Sydney, Australia
    Posts
    281
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by felgall
    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?
    Last edited by itsonlybarney; 05-05-2006 at 04:22 AM.

  • #7
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by itsonlybarney
    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.
    Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •