Update tables problem

Discussion in 'PHP' started by zerokvl, May 20, 2011.

  1. #1
    Hi to all. I have a little problem with my update script. I want to update all my tables at every 4 hours every day. I have the tables, table1, table2 and table3 and every table has row status="2". Now i want to verify every table and where is status="2" to update it with status="1".
    I made a foreach to verify every table but i have a little problem, i get an error like:

    You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1
    PHP:
    It seens that my problem is that, in table1 i don`t have any rows with status="2" and is not going to verify the second table. If i add on a row, at table1 with status="2" it works but after it updates, it doesn`t work for the others tables. I will post the script, and if you can help me, please tell me, because i need it very much.


    $date = date('j-m-Y');
    $hour = date('h:i:s');
    $exactTime = time();
    $updateTime = $exactTime+14400;
    
    $limbi_query = mysql_query("SELECT * from allTables");
    $table = array ();
    while($rand = mysql_fetch_assoc($limbi_query)){
    $table[] = array (
    'table' => $rand['table']
    );
    }
    foreach ($table as $limb) {
    
    $select = mysql_query("SELECT * from ".$limb['table']." where status='2' ORDER by id ASC limit 1");
    WHILE($rand = mysql_fetch_array($select)){
    $id = $rand['id'];
    $gametime = $rand['time'];
    }
    
    if($exactTime > $gametime){
    //here is updateing the tables with status="1"
    $update = mysql_query("UPDATE ".$limb['table']." SET status='1',data='$date',ora='$hour' WHERE id = $id") or die(mysql_error());
    echo "Jocul a fost adaugat cu succes!<br>";
    //select the second row with status="2"
    $select2 = mysql_query("SELECT * from ".$limb['table']." WHERE status = '2' ORDER by id ASC limit 1");
    WHILE($rand2 = mysql_fetch_array($select2)){
    $id2 = $rand2['id'];
    }
    //update for the second row with 4 hours +
    $update2 = mysql_query("UPDATE ".$limb['table']." SET time='$updateTime' WHERE id = $id2") or die(mysql_error());
    echo "The second row will be updated after 4 hours";
    
    }
    else{
    echo "Sorry but i can`t update the table only after 4 hours.";
    }
    
    }
    PHP:
    The code is a little bit messy but the problem is on my first query $update. There if i don`t have status="2" on my first table, it stops there with the error above.
    Hope you understand me, and please sorry for my bad language.
     
    zerokvl, May 20, 2011 IP
  2. Aotearoa

    Aotearoa Member

    Messages:
    40
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    43
    #2
    Not sure that this is the problem, but is you have no rows in the table with status='2' you'll never go through the while loop and $id / $id2 as appropriate will never be set.

    This would mean that you're making a where clause that reads "where id = ". If this is the clause, the near '' in the error message is indicating the end of the string, shame it isn't a more helpful message.

    HTH

    Bruce
     
    Aotearoa, May 20, 2011 IP
  3. zerokvl

    zerokvl Peon

    Messages:
    19
    Likes Received:
    0
    Best Answers:
    1
    Trophy Points:
    0
    #3
    Solved. I moved $select up with some lines and i did a mysql_num_rows conditions, and it works :D
    Thank you for your response.
     
    zerokvl, May 20, 2011 IP