cant update mysql table

Discussion in 'PHP' started by PedroG, Dec 3, 2010.

  1. #1
    Hi
    I have some problem with a php file i´me trying to use as a cron job. When i execute the php file (through browser) it runs perfectly, but nothing happens in the DB.... (a few hours ago, i inserted the mysql_error to try to find out the problem, but still can´t understand it....

    This is the code.... (username, password, and table info "hidden")

    
        <?php
        error_reporting(E_ALL);
        $con = mysql_connect("localhost","username","password");
        if (!$con)
         {
         die('Could not connect: ' . mysql_error());
         }
         
        mysql_select_db("account_table", $con) or die(mysql_error());
        echo "connected";
         
         
        mysql_query("UPDATE phcdl_users SET user_uploads = (SELECT COUNT(file_id) FROM phcdl_files WHERE phcdl_files.file_author = phcdl_users.user_name GROUP BY user_id"); echo "<br/>1# done";
        mysql_query("UPDATE phcdl_users SET total_downs = (SELECT SUM(file_downloads) FROM phcdl_files WHERE phcdl_users.user_uploads > '0' AND  phcdl_files.file_author = phcdl_users.user_name GROUP BY user_id");echo "<br/>2# done";
        mysql_query("UPDATE phcdl_users SET total_cmmnts = (SELECT SUM(file_total_comments) FROM phcdl_files WHERE phcdl_users.user_uploads > '0' AND  phcdl_files.file_author = phcdl_users.user_name GROUP BY user_id");echo "<br/>3# done";
        mysql_query("UPDATE phcdl_users SET total_views = (SELECT SUM(file_fiews) FROM phcdl_files WHERE phcdl_users.user_uploads > '0' AND phcdl_files.file_author = phcdl_users.user_name GROUP BY user_id") ;echo "<br/>4# done";
        mysql_query("UPDATE phcdl_users SET total_votes = (SELECT SUM(file_votes) FROM phcdl_files WHERE phcdl_users.user_uploads > '0' AND phcdl_files.file_author = phcdl_users.user_name GROUP BY user_id");echo "<br/>5# done";
         echo mysql_errno($con) . ": " . mysql_error($con) . "\n";
        mysql_close($con);
        ?> 
    Code (php):
    Which output this :
    connected
    1# done
    2# done
    3# done
    4# done
    5# done
    1064: 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 
    Code (markup):
    All the SQL commands run without any problem on PhpMyAdmin. if anyone can help me with the problem here i´d appreciate a lot :)
     
    PedroG, Dec 3, 2010 IP
  2. xpertdev

    xpertdev Peon

    Messages:
    54
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    use mysql_error as below, so you will get which query is causing the error.

    if(!mysql_query($query))
    {
    echo mysql_error();
    }
     
    xpertdev, Dec 3, 2010 IP
  3. digiklan

    digiklan Member

    Messages:
    25
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    38
    #3
    Try to use numbers without inverted commas (phcdl_users.user_uploads > '0'), like (phcdl_users.user_uploads > 0) if it is integer and you send it like a string that can cause a problem.
     
    digiklan, Dec 5, 2010 IP
  4. w47w47

    w47w47 Peon

    Messages:
    255
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    do as xpertdev said and post the error here.
     
    w47w47, Dec 5, 2010 IP
  5. PedroG

    PedroG Member

    Messages:
    59
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    43
    #5
    Thanks,
    I´ve tried that but i still get an error related to the 1st line.....:confused:

    connected
    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
    Code (markup):
    <?php
    $con = mysql_connect('localhost','account','password');
    mysql_select_db("table", $con);
    echo "connected<br/>";
    
    $q1 = "UPDATE phcdl_users SET user_uploads = (SELECT COUNT(file_id) FROM phcdl_files WHERE phcdl_files.file_author = phcdl_users.user_name GROUP BY user_id";
    if(!mysql_query($q1))
    {
    echo mysql_error();
    } 
    
    
    mysql_close($con);
    ?>
    Code (PHP):

    I´ve just used one query now to try to fix the problem with a smaller code.... (all the queries are based on the first one, so if the error is on the query the 1st one is enough xD)

    I also removed the commas like you said, digiklan (before trying the code above) but got the same error....
     
    PedroG, Dec 6, 2010 IP
  6. Simple Management

    Simple Management Peon

    Messages:
    39
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #6
    You are missing one bracket at the end of your query ( SELECT statement ).
     
    Simple Management, Dec 6, 2010 IP
  7. w47w47

    w47w47 Peon

    Messages:
    255
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    try to put phcdl_users in the mysql quotes like this: `phcdl_users`
     
    w47w47, Dec 6, 2010 IP
  8. PedroG

    PedroG Member

    Messages:
    59
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    43
    #8
    i think it´s working fine now, thank you
    (don´t know how i missed it xD because i´ve tried to put the bracket, but guess i were trying at the wrong place xD)
     
    PedroG, Dec 20, 2010 IP