Jump to content

Add Value To Column On Daily Basis (SOLVED)


Usarjjaco

Recommended Posts

Hello Everyone,Looking to find out how I would perform the following:Every 24 hours have column "age" update by +1 for every row in that column. I've taken stabs at it but haven't quite latched it yet. I'm assuming this is done with making an event happen... I am pretty sure I know how to have an event occur daily; but I need to know what code I need to write to make the event add 1 to each rows values in a certain column. Any help would be greatly appreciated.Thanks in advance,JJ

Link to comment
Share on other sites

UPDATE table SET age = age + 1You can use tools like cron or Windows Task Scheduler to schedule things to run.
I put the job into chronjobs; but is there some special code I need to use in the cronjob command? I entered the command using my table name and column name; but is there some way I Need to let chron jobs know which database to pull from?
Link to comment
Share on other sites

You need to point cron to a program to run, you don't just type code into cron. Create a script to do the database update and have cron execute the script. On my server, I've used cron to execute MySQL and I've given MySQL a filename to execute through the command line, and I've also used cron to execute PHP and sent it a PHP script filename to execute.

Link to comment
Share on other sites

You need to point cron to a program to run, you don't just type code into cron. Create a script to do the database update and have cron execute the script. On my server, I've used cron to execute MySQL and I've given MySQL a filename to execute through the command line, and I've also used cron to execute PHP and sent it a PHP script filename to execute.
Ok,So I'm looking at the command list for *nix and not sure what to use; so if I wanted to run the script "dayupdate.php" which is located in my "local_host/v/" directory how would I do that? Thanks again :)
Link to comment
Share on other sites

I can't tell you how your server is configured, but this command is similar to what I use:php -f /home/<acct>/public_html/sources/process_mail_queue.phpmysql -h localhost -D <dbname> -u <dbuser> -p<dbpass> < /home/<acct>/public_html/sql/copy_records.sqlYou can find information about how to run PHP and MySQL from the command line in their respective manuals.

Link to comment
Share on other sites

I can't tell you how your server is configured, but this command is similar to what I use:php -f /home/<acct>/public_html/sources/process_mail_queue.phpmysql -h localhost -D <dbname> -u <dbuser> -p<dbpass> < /home/<acct>/public_html/sql/copy_records.sqlYou can find information about how to run PHP and MySQL from the command line in their respective manuals.
Ok I'll give it a whirl and see what I come up with :) Thanks
Link to comment
Share on other sites

Ok I'll give it a whirl and see what I come up with :) Thanks
What is the -f for. I think it's for selecting a file; however when I input this I get an error saying it's not an option.I entered my account name in the <acct> bracket. ... I am using CGI and I included the CGI line at the beginning of my PHP script.
Link to comment
Share on other sites

Ok,I have set up the dayupdate.php with this linemysql_query("UPDATE horsesgeneral SET age=age + 1");and then set up chron jobs for/home/myself/public_html/directory/dayupdate.phpI read that you don't need the -f as it will execute a file location on it's own. I'm guessing that it isn't working because I did something wrong in the query line...JJ
Link to comment
Share on other sites

You should see an error message if there's a problem. Run this on the command line first before using it with cron, that way you don't have to guess what the error is.
Ok fixed it .. or almost anyways :) . I have it functioning now; but instead of adding 1 to each of the rows in that column it makes all rows in the column go to 0 upon the chron job update... what am I doing wrong?Thanks for all the help,JJ
Link to comment
Share on other sites

I'm not sure, that query will add 1 to each age field in all rows. You can run that query directly on phpMyAdmin if you want to verify that, but that query alone isn't going to set all of the values to 0.
Yes I agree, I just ran it in php my admin and when I run the query there it functions properly. ... and that is the only query line I run in the entire script page... so I'm kind of at a loss; I just have the chron job command pointing directly to the file location for the script.
Link to comment
Share on other sites

And when you open the script in a browser it runs correctly?
Yes when I go directly to the .php script page in my browser I get no error messages; just the plain white background.I've been tinkering with chronjobs and my settings but can't figure it out; right now here is what I have for my entire php script
#!/usr/local/bin/php -q<?php//Initialize Server SQL Connection Or Show Error$con = mysql_connect("localhost","theripti","EmmyBarla1025!!");if (!$con)  {  die('Could not connect: ' . mysql_error());  }//Connect To Databasemysql_select_db("theripti_jml5", $con);mysql_query("UPDATE horsesgeneral SET age = age + '1'");?>

and then for chronjobs I've used bothphp - f /home/username/public_html/directoryname/dayageupdate.phpand/usr/local/bin/php -q /home/username/public_html/directoryname/dayageupdate.phpI can manually go into mysql and update the values using the query; but every time the chronjob activates all values reset to 0.

Link to comment
Share on other sites

Yes when I go directly to the .php script page in my browser I get no error messages; just the plain white background.
And the database gets updated?You also have the 1 in quotes. You should be adding a number, not a string.
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...