.Bat to run .php page !help!

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

User avatar
Goofan
Forum Contributor
Posts: 305
Joined: Wed Nov 04, 2009 2:11 pm
Location: Sweden

.Bat to run .php page !help!

Post by Goofan »

Hi i need some help as i dont understand cron...

i need to make so that everytime the clock hits 00:30 or 01:00 (every 30 min) it will uppdate a datatable and add value into it. it will do this every 30 min. and ive been told to use Cron. I dont understand Cron that much so i cant make the "function" im after. plz help



-Thanks in advance
-Thomas
Last edited by Goofan on Tue Dec 01, 2009 12:30 pm, edited 1 time in total.
User avatar
Grizzzzzzzzzz
Forum Contributor
Posts: 125
Joined: Wed Sep 02, 2009 8:51 am

Re: Cron!!

Post by Grizzzzzzzzzz »

might be worth looking at forums dedicated to Cron
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Cron!!

Post by AbraCadaver »

Not really PHP because you don't need it here, but here is a solution that can use PHP.

You either edit /etc/crontab or create a crontab for a specific user by doing 'crontab -e'. Then add the following:

Code: Select all

0,30   *   *   *    *  /path/to/php /path/to/phpfile.php
Then in /path/to/phpfile.php add the PHP/SQL to do the update.

To skip PHP (assuming you're using MySQL), just add the SQL to the crontab:

Code: Select all

0,30   *   *   *    *   mysql -h hostname -u username -ppassword -e "UPDATE mytable SET field = 'value' WHERE something = 'something'"
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
User avatar
Goofan
Forum Contributor
Posts: 305
Joined: Wed Nov 04, 2009 2:11 pm
Location: Sweden

Re: Cron!!

Post by Goofan »

AbraCadaver wrote:Not really PHP because you don't need it here, but here is a solution that can use PHP.

You either edit /etc/crontab or create a crontab for a specific user by doing 'crontab -e'. Then add the following:

Code: Select all

0,30   *   *   *    *  /path/to/php /path/to/phpfile.php
Then in /path/to/phpfile.php add the PHP/SQL to do the update.

To skip PHP (assuming you're using MySQL), just add the SQL to the crontab:

Code: Select all

0,30   *   *   *    *   mysql -h hostname -u username -ppassword -e "UPDATE mytable SET field = 'value' WHERE something = 'something'"
this part:
-h hostname -u username -p password
is it to connect to the database or something else?
User avatar
Goofan
Forum Contributor
Posts: 305
Joined: Wed Nov 04, 2009 2:11 pm
Location: Sweden

Re: Cron!!

Post by Goofan »

and if its the "connect to database shouldnt database.... be in there aswell...

exampel:
$dbhost = "localhost";
$dbuser = "root";
$dbpass = "";
$dbname = "this"; <-------this as -n this ?




Else would this be correct?

0,30 * * * * mysql -h localhost -u root -p -e "UPDATE konto SET field = 'pengar' WHERE pengar = 'pengar' + 30000"
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Cron!!

Post by AbraCadaver »

Goofan wrote:and if its the "connect to database shouldnt database.... be in there aswell...

exampel:
$dbhost = "localhost";
$dbuser = "root";
$dbpass = "";
$dbname = "this"; <-------this as -n this ?




Else would this be correct?

0,30 * * * * mysql -h localhost -u root -p -e "UPDATE konto SET field = 'pengar' WHERE pengar = 'pengar' + 30000"
Your query doesn't look correct, but I don't know what you're doing so I can't say. You can add the db in the query: UPDATE dbname.konto
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
User avatar
Goofan
Forum Contributor
Posts: 305
Joined: Wed Nov 04, 2009 2:11 pm
Location: Sweden

Re: Cron!!

Post by Goofan »

found a temporary way to solve this "only got one problem"

ive made so that i run it throught windows scheduled task manager (as this is so far only a home edition(the thing im trying to do))... it runs the script every 30 min.
my only problem is i dont know what to type as it runs "a program" i guess i have to make a .bat file. so i made a .bat file but i still dont know what to type into hte .bat file is it that i should make it to start a .php page with the information (uppdate ..........).
Do u think that would work? im about to try but if u think it wount then tell me :D
Last edited by Goofan on Tue Dec 01, 2009 12:52 pm, edited 1 time in total.
User avatar
Goofan
Forum Contributor
Posts: 305
Joined: Wed Nov 04, 2009 2:11 pm
Location: Sweden

Re: Cron!!

Post by Goofan »

ok so that didnt really work...
i told a bat file this:
content of .bat:

Code: Select all

 
C:\Program Files\wamp\www\www\Projektarbete\Uppdate_money.php
 
and i set into Uppdate_money.php:

Code: Select all

 
<?php
include "database.php";//Fil för databasinställning.
 
    // Build our query
    $sql1 = "SELECT * FROM konto";
    $result = mysql_query($sql) or die(mysql_error());//Välj all info i tall. //hämtar all info från tabell
                 
while($row = mysql_fetch_array( $result )) //hämtar info från tabell.
{
    $pengar = $row['pengar'];
}
$sql="UPDATE konto SET pengar=($pengar + 30000)";//Sätt upp SQL fråga.
?>
 
see anything i should change or make diffrent?

im not totaly sure about the .bat file if its correct with its content...


-Thanks in advance
-Thomas
Last edited by Goofan on Wed Dec 02, 2009 7:41 am, edited 1 time in total.
User avatar
Goofan
Forum Contributor
Posts: 305
Joined: Wed Nov 04, 2009 2:11 pm
Location: Sweden

Re: .Bat to run .php page !help!

Post by Goofan »

any help appreciated:D
i runned it and well nothing happend...
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: .Bat to run .php page !help!

Post by John Cartwright »

Hmm.. stick with your other thread (you were headed in the right direction) and this is basically a duplicate.
User avatar
Goofan
Forum Contributor
Posts: 305
Joined: Wed Nov 04, 2009 2:11 pm
Location: Sweden

Re: .Bat to run .php page !help!

Post by Goofan »

which one do u mean and how do u mean? plz help...
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: .Bat to run .php page !help!

Post by John Cartwright »

What I meant was your best bet is to use cron (linux) or task scheduler (windows) to directly invoke the CLI (Command Line Interface). Not need for a .bat file -- however -- you can find an example in the manual (scroll down to "Example #9 Batch file to run a command line PHP script (script.bat)").

You simply don't pass the location of the PHP file, since the file itself needs to be interpreted by the PHP engine. Take a look here on how you would call the your program in CLI.

I suggest you let me know which one of your threads you want me to close, since they are both for the same issue.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: .Bat to run .php page !help!

Post by AbraCadaver »

I told myself I was going to leave this thread, but...

First, you need to learn something about what you're doing in PHP and SQL. What you have in the PHP file is never going to work. Second, something like this in the batch file should work:

Code: Select all

c:\path\to\mysql -h localhost -u root -e "UPDATE dbname.konto SET pengar = (pengar + 30000)"
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
User avatar
Goofan
Forum Contributor
Posts: 305
Joined: Wed Nov 04, 2009 2:11 pm
Location: Sweden

Re: .Bat to run .php page !help!

Post by Goofan »

John Cartwright wrote:What I meant was your best bet is to use cron (linux) or task scheduler (windows) to directly invoke the CLI (Command Line Interface). Not need for a .bat file -- however -- you can find an example in the manual (scroll down to "Example #9 Batch file to run a command line PHP script (script.bat)").

You simply don't pass the location of the PHP file, since the file itself needs to be interpreted by the PHP engine. Take a look here on how you would call the your program in CLI.

I suggest you let me know which one of your threads you want me to close, since they are both for the same issue.

besides cant get "any" of the two "idés to work :( (.bat idés)

You can close the other one :D
User avatar
daedalus__
DevNet Resident
Posts: 1925
Joined: Thu Feb 09, 2006 4:52 pm

Re: .Bat to run .php page !help!

Post by daedalus__ »

oh now i know why you locked it. :|

goofan, its daedalus!

where are you at with this? im here to help.
Post Reply