Changing a database value at a certain time.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
kryppienation
Forum Newbie
Posts: 9
Joined: Thu Dec 04, 2008 1:12 am

Changing a database value at a certain time.

Post by kryppienation »

I am trying to set a certain field "daily" to reset to the value of 200 in an entire table throughout all records. I am not sure how to work with time. I would like to also watch the time remaining until the reset happens... Now this time does not have to live, just a post of the time remaining that updates when i refresh is quite fine. I am not sure if i need to make another database table to hold the information or not, i am sure that i most likely do... which is completely fine. I am wondering if someone knows how i can do this?

I only need to do this one time in a 24 hours period. Like once a midnight... i don't know if that makes it easier or not... but perhaps a bit less confusing. Basically i need to know how to run an update statement every 24 hours.

EDIT: I use windows OS too. Apache and MySQL if that matters at all.

Thanks Everyone! :lol:
KN
Mark Baker
Forum Regular
Posts: 710
Joined: Thu Oct 30, 2008 6:24 pm

Re: Changing a database value at a certain time.

Post by Mark Baker »

You write a simple php script that you can run from the command line to do the task, then use Windows scheduled tasks to run that script at the time and frequency of your choosing
xQuasar
Forum Newbie
Posts: 19
Joined: Tue Dec 02, 2008 5:53 pm

Re: Changing a database value at a certain time.

Post by xQuasar »

Is there a way to do it just with PHP scripts, no other programs involved? I'm also interested in this, I'm looking for a way to make a column increase in value every half hour (for every row).
kryppienation
Forum Newbie
Posts: 9
Joined: Thu Dec 04, 2008 1:12 am

Re: Changing a database value at a certain time.

Post by kryppienation »

Ok I have done that, it still didn't change the values in my database. here is what i got.


this is all of the stuff in "RUN:"

C:\xampp\php\php.exe C:\xampp\htdocs\kryppienation\admin\changemotivation.php


it opens the php.exe CLI but it's not reading the file... i also tried:

C:\xampp\php\php.exe changemotivation.php
when i did this i made sure that the changemotivation.php and the included db.php were in the c:\xampp\php folder.

didn't work either

Thanks.


here is the code in the php file.

Code: Select all

<?php
 
 
 
include_once('../includes/db.php');
 
$setmotivation = 'update knusers set motivation = "200"';
 
 
 
DB::connect($DB_database);
DB::query($setmotivation, "Name of query for error handling");
DB::close();
 
 
?>
Attachments
schedule.PNG
schedule.PNG (23.23 KiB) Viewed 1268 times
Mark Baker
Forum Regular
Posts: 710
Joined: Thu Oct 30, 2008 6:24 pm

Re: Changing a database value at a certain time.

Post by Mark Baker »

xQuasar wrote:Is there a way to do it just with PHP scripts, no other programs involved? I'm also interested in this, I'm looking for a way to make a column increase in value every half hour (for every row).
Not unless you have that PHP script permanently running in the background... and you can't do that from a web page.
Cron, or its equivalent on whatever your operating system is, is the best way to do it.
Mark Baker
Forum Regular
Posts: 710
Joined: Thu Oct 30, 2008 6:24 pm

Re: Changing a database value at a certain time.

Post by Mark Baker »

kryppienation wrote:Ok I have done that, it still didn't change the values in my database. here is what i got.
Make sure your start directory is correct, or have the script chdir to the correct directory as the first thing it does.
Run phpinfo from the command line to make sure you're picking up the correct php.ini file (to ensure that your php.ini path is correctly set). [I believe xampp has different php.ini files for web and command line use]
kryppienation
Forum Newbie
Posts: 9
Joined: Thu Dec 04, 2008 1:12 am

Re: Changing a database value at a certain time.

Post by kryppienation »

what should the start directory be pointing to? I am not familiar with how to use the CLI properly. What would i need to type into there to check the php.ini file it is reading? Please forgive me, this is the first time i have ever used this and i am trying to get this to work very badly.


Thank you :D

EDIT: after checking in the folders i found this...

there is a php.ini file in the C:\xampp\apache\bin (in my webserver check of the phpinfo() i get this location)

there is a php.ini file in the C:\xampp\php (this is where the CLI is located, i don't know how to check, but my guess the CLI reads this one)

so... i know that much. can you help me to solve what i need to do to make it work correctly?
Mark Baker
Forum Regular
Posts: 710
Joined: Thu Oct 30, 2008 6:24 pm

Re: Changing a database value at a certain time.

Post by Mark Baker »

From the command line:

Code: Select all

\xampp\php\php -r phpinfo();
The -r allows you to execute PHP code passed in directly on the command line. In this case, executing the phpinfo() function.
If your Windows command line buffer can't handle all this output, use

Code: Select all

\xampp\php\php -r phpinfo(); > phpinfo.txt
to write the output to a text file.
You can then read that output to get all the information about your php configuration, including which php.ini file it's using, and the path settings.
Mark Baker
Forum Regular
Posts: 710
Joined: Thu Oct 30, 2008 6:24 pm

Re: Changing a database value at a certain time.

Post by Mark Baker »

kryppienation wrote:what should the start directory be pointing to?
Given that your code has the following line:

Code: Select all

include_once('../includes/db.php');
Then it needs to be executing in a directory where this relative path would be valid. I'd expect this to be in the "C:\xampp\htdocs\kryppienation\admin" directory
kryppienation
Forum Newbie
Posts: 9
Joined: Thu Dec 04, 2008 1:12 am

{SOLVED}Re: Changing a database value at a certain time.

Post by kryppienation »

Man you rock thanks for all your help. Case closed, works!!!


Much Appreciation, :lol:
~KN
Mark Baker
Forum Regular
Posts: 710
Joined: Thu Oct 30, 2008 6:24 pm

Re: Changing a database value at a certain time.

Post by Mark Baker »

It's very useful to know how to use the command line.... and I highly recommend that every PHP developer should read the appropriate manual pages

I reckon that nearly half the problems that people here ask for help with could be identified (by validating the syntax of a file using "php -l <filename>" from the command line).
Post Reply