Ask a Question related to PHP Development, Design and Development.

  1. #1

    Default cron problem

    Hi,

    I've got a problem with a cron fucntion.
    I have a mysql db which I want to be cleaned every 12 hours. So I decided to
    use a cron.
    There is a problem. When I execute it, I've got the error:

    "Database error: Invalid SQL: DELETE FROM myng_ref_proxad_free_adsl WHERE
    id_article IN ()
    MySQL Error: 1064 (You have an error in your SQL syntax near ')' at line 1)
    Database error: Invalid SQL: DELETE FROM myng_library WHERE lib_art_id IN ()
    MySQL Error: 1064 (You have an error in your SQL syntax near ')' at line 1)"

    If I try execute it again, it works without any problem.
    So why do I have to execute it two times in order not to obtain mysql error?

    You could tell me "so execute it two times!"

    But the problem is that cron can not do it. It just tries to execute it one
    time every 12 hours and it gives an error...

    Can you help me?

    Thanksfully,

    Ludo



    This is the script I execute:

    <?
    // Iniciamos la sesión!!
    session_start();

    ini_set("max_execution_time",600);

    include("../config.php");

    // Need DB Connection
    $db=new My_db;
    $db->connect();

    $db2=new My_db;
    $db2->connect();

    // MyNG setting up...
    init();

    // Set up the language
    modules_get_language();

    // Templates
    $t = new
    Template($_SESSION['conf_system_root']."/themes/".$_SESSION['conf_vis_theme']."/templates/");


    // Check if clean up ALL the groups or just the
    // configured ones
    if($_SESSION['conf_clean_MAX_days'] == 0 &&
    $_SESSION['conf_clean_MAX_articles'] == 0){


    $query = "SELECT
    grp_name,grp_id,grp_num_messages,grp_MAX_articles, grp_MAX_days FROM
    myng_newsgroup WHERE grp_MAX_articles != 0 OR grp_MAX_days != 0";
    $db->query($query);


    // For each newsgroup
    while($db->next_record()){

    // Articles!
    if($db->Record['grp_MAX_articles'] != 0){

    // Delete (M - N)articles
    // M = articles at the group
    // N = MAX Articles
    if($db->Record['grp_num_messages'] >
    $db->Record['grp_MAX_articles']){
    $num_2_delete = ($db->Record['grp_num_messages'] -
    $db->Record['grp_MAX_articles']);
    }else{
    $num_2_delete = 0;
    }

    //echo $num_2_delete."<br>";
    if($num_2_delete != 0){
    del_articles($num_2_delete,$db->Record['grp_name']);
    }

    }
    // Days!
    if($db->Record['grp_MAX_days'] != 0){

    // Get current timestamp
    $now = time();
    $num_days = $db->Record['grp_MAX_days'];

    // Get the limit timestamp
    $limit = $now - ($num_days * 24 * 60 * 60);

    $group_name = real2table($db->Record['grp_name']);
    $query_2 = "SELECT count(*) FROM `"."myng_".$group_name."` WHERE
    date < ".$limit;
    //echo $query_2;
    $db2->query($query_2);
    $db2->next_record();

    $num_2_delete = $db2->Record[0];

    if($num_2_delete != 0){
    del_articles($num_2_delete,$db->Record['grp_name']);
    }

    }

    }

    }else{

    // All the groups !
    // Check if we depend on number of days or just articles

    // Days!
    if($_SESSION['conf_clean_MAX_days'] != 0){

    // Get current timestamp
    $now = time();
    $num_days = $_SESSION['conf_clean_MAX_days'];

    // Get the limit timestamp
    $limit = $now - ($num_days * 24 * 60 * 60);

    // Get all the groups

    $query = "SELECT grp_name,grp_id FROM myng_newsgroup";
    $db->query($query);

    // For each newsgroup
    while($db->next_record()){

    $group_name = real2table($db->Record['grp_name']);
    $query_2 = "SELECT count(*) FROM `"."myng_".$group_name."` WHERE
    date < ".$limit;
    //echo $query_2;
    $db2->query($query_2);
    $db2->next_record();

    $num_2_delete = $db2->Record[0];

    if($num_2_delete != 0){
    del_articles($num_2_delete,$db->Record['grp_name']);
    }

    }

    }

    // Articles!
    if($_SESSION['conf_clean_MAX_articles'] != 0){

    // Get all the groups

    $query = "SELECT grp_name,grp_id,grp_num_messages FROM myng_newsgroup";
    $db->query($query);

    // For each newsgroup
    while($db->next_record()){

    // Delete (M - N)articles
    // M = articles at the group
    // N = MAX Articles
    if($db->Record['grp_num_messages'] >
    $_SESSION['conf_clean_MAX_articles']){
    $num_2_delete = ($db->Record['grp_num_messages'] -
    $_SESSION['conf_clean_MAX_articles']);
    }else{
    $num_2_delete = 0;
    }

    if($num_2_delete != 0){
    del_articles($num_2_delete,$db->Record['grp_name']);
    }

    }

    }

    }


    // Check if the cleanup has been FORCED

    if(isset($_POST['apply_cleanup']) && $_POST['apply_cleanup'] == "yes"){
    // We've done up to now the clean up, just return to the
    // refering page
    header ("Location: ".$_POST['return_page']);
    //echo $_POST['return_page'];
    }

    ?>


    ludo Guest

  2. Similar Questions and Discussions

    1. cron job
      If the cron job run every 15 minute, it will fill up mail box. Does anyone has any idea. Thanks "Davide Bianchi" <davideyeahsure@onlyforfun.net>...
    2. rsh and cron
      I have Ultra 60 with Solaris 2.6, and the rsh always work fine, until recently I foud the rsh does not work on the Ultra 60. But I don't have any...
    3. cron
      Not specific to PHP really, but it all started with a simple PHP script I was automating... I've used cron/crontab on a zillion machines, and it...
    4. CRON: bad user
      Hi all, I just noticed the same messages in the cron-log: ! bad user (root) After reading this topic I looked at the shadow-file. I noticed...
    5. Problem using cron on SCO 5.0.5
      Hi, I have a problem using cron on Compaq Proliant ML-370 O/S SCO 5.0.5.. Jobs have been scheduled for more than 20hrs. Problem is after executing...
  3. #2

    Default Re: cron problem

    ludo wrote:
    > Hi,
    >
    > I've got a problem with a cron fucntion.
    > I have a mysql db which I want to be cleaned every 12 hours. So I decided
    > to use a cron.
    > There is a problem. When I execute it, I've got the error:
    >
    > "Database error: Invalid SQL: DELETE FROM myng_ref_proxad_free_adsl WHERE
    > id_article IN ()
    > MySQL Error: 1064 (You have an error in your SQL syntax near ')' at line
    > 1) Database error: Invalid SQL: DELETE FROM myng_library WHERE lib_art_id
    > IN () MySQL Error: 1064 (You have an error in your SQL syntax near ')' at
    > line 1)"
    These weirdo cron errors are usually because cron isn't loading/passing
    correct environment variables to the script, or is running as the wrong
    user.

    Cron usually runs as root and so most cron jobs are also run as root. If
    your script runs fine running under your user ID, then write a simple
    wrapper script and get cron to execute the wrapper instead:

    #!/bin/bash
    su - your_userID -c "your_mysql_script.php"

    Note the little "-" between su and your userID, this tells su to load your
    environment before executing the command(s) between the "" after the -c.

    The other option might be to run your script as a user cron job. To do
    this, just log in as yourself and type "crontab -e" to edit your personal
    cron jobs :) These will run (surprise, surprise) as 'you' when executed,
    usually negating the need to use a wrapper with root's cron jobs.

    man su
    man cron

    Cheers,

    James
    --
    MESSAGE ACKNOWLEDGED -- The Pershing II missiles have been launched.

    Centurion Guest

Posting Permissions

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139