drmota Posted June 17, 2011 Share Posted June 17, 2011 Hello, So i get data stored onto my computer all the time. I parse this data once an hour, but then I need it to get into my MySQL database somehow. I chose PHP to do this for me. Now, I am a Linux Ubuntu 11.04 user and I have written a simple PHP script which inserts the data i need it to into a database. The connection and SQL query are in the PHP script. I will need this script to run once every hour. For this I will be using the cron job. Does anyone know what to write in the terminal to create a cron job, which runs through the PHP script and connects to the MySQL database, and eventually inserting data into a table? Thanks in advance Quote Link to comment https://forums.phpfreaks.com/topic/239638-cron-job-mysql-in-php/ Share on other sites More sharing options...
boompa Posted June 17, 2011 Share Posted June 17, 2011 Here's a link for you on the basics. You can either make the script executable and add a #!/path/to/php at the top of the file to enable you to do the following in the crontab: 0 * * * * /path/to/script 1>/file/to/log/stuff/to 2>&1 or instead add the path to PHP and the script to the command: 0 * * * * /path/to/php -f /path/to/script 1>/file/to/log/stuff/to 2>&1 Quote Link to comment https://forums.phpfreaks.com/topic/239638-cron-job-mysql-in-php/#findComment-1231002 Share on other sites More sharing options...
gristoi Posted June 17, 2011 Share Posted June 17, 2011 if your php script contains your mysql connection details and a valid connection the the database then all you need to do is point a cron to your script: 1. open a terminal 2. open a cron editor, i use crontab -e 3: use the following format : taken from http://adminschoice.com/crontab-quick-reference * * * * * command to be executed - - - - - | | | | | | | | | +----- day of week (0 - 6) (Sunday=0) | | | +------- month (1 - 12) | | +--------- day of month (1 - 31) | +----------- hour (0 - 23) +------------- min (0 - 59) so for a php script that runs at 17:00 every day: * 17 * * * php /path/to/your/script.php Quote Link to comment https://forums.phpfreaks.com/topic/239638-cron-job-mysql-in-php/#findComment-1231004 Share on other sites More sharing options...
drmota Posted June 17, 2011 Author Share Posted June 17, 2011 Hello thanks for the replies! However get an error each time I try to run the script: PHP Fatal error: Call to undefined function mysql_connect(). I have installed Lampp for my distro and it works fine. I can even view the file from my browser (it's in my htdocs) and it works fine from there. But I need it to be able to run like this: php script.php Without getting the error I have verified by sudo which php That #!/usr/bin/php is the correct path to my php, but I still get that error. I hope this can get figured out! Thanks a lot! Quote Link to comment https://forums.phpfreaks.com/topic/239638-cron-job-mysql-in-php/#findComment-1231040 Share on other sites More sharing options...
gristoi Posted June 17, 2011 Share Posted June 17, 2011 post your script here Quote Link to comment https://forums.phpfreaks.com/topic/239638-cron-job-mysql-in-php/#findComment-1231047 Share on other sites More sharing options...
drmota Posted June 17, 2011 Author Share Posted June 17, 2011 <?php #!/usr/bin/php mysql_connect('localhost','usrname','pw') or die ('Error connecting'); ?> And the error is: PHP Fatal error: Call to undefined function mysql_connect() in /dir/file.php on line 4 Thanks for cooperating Quote Link to comment https://forums.phpfreaks.com/topic/239638-cron-job-mysql-in-php/#findComment-1231075 Share on other sites More sharing options...
gristoi Posted June 17, 2011 Share Posted June 17, 2011 Have u checked that your mysql is installed? and that the extension is enabled correctly. what happens when you open up a terminal window in ubuntu and type mysql -v Quote Link to comment https://forums.phpfreaks.com/topic/239638-cron-job-mysql-in-php/#findComment-1231078 Share on other sites More sharing options...
drmota Posted June 17, 2011 Author Share Posted June 17, 2011 When I type mysql -v I get 'the program cannot be found' and it shows me packages for installation. Will this be able to connect to the databases which I have on my mysql server installed with lampp? To the first question, I installed mysql with lampp, how do I configure it into this script? Thanks so much Quote Link to comment https://forums.phpfreaks.com/topic/239638-cron-job-mysql-in-php/#findComment-1231126 Share on other sites More sharing options...
gristoi Posted June 17, 2011 Share Posted June 17, 2011 Well, it's telling u that u do not have mysql installed. How exactly did u install your 'lamp' server ? I would first google how to install and configure a lamp server, then once you Definately have php AND mysql installed, retry your connection. If it still does not work , repost here Quote Link to comment https://forums.phpfreaks.com/topic/239638-cron-job-mysql-in-php/#findComment-1231138 Share on other sites More sharing options...
drmota Posted June 17, 2011 Author Share Posted June 17, 2011 I have been using lampp for several years, I have decent knowledge in how to set it up. As I posted before, I am able to run the script through the browser (meaning it is configured and all). What I need is a MySQL package on my computer. I will use one of the two which were advised to me by the terminal. However, will this be able to connect to the MySQL database I have on my lampp server i.e. the one at http://localhost/phpmyadmin/ ? thanks Quote Link to comment https://forums.phpfreaks.com/topic/239638-cron-job-mysql-in-php/#findComment-1231147 Share on other sites More sharing options...
gristoi Posted June 17, 2011 Share Posted June 17, 2011 Is your lamp server on the same pc u are trying run the php script from. And I don't want to sound rude but if you've been using and building lamp stacks fe years you would know that phpmyadmin is only a GUI for the mysql server. Which means that if it is on your pc then you shouldn't be having this problem as running via command line is making exactly the call to the mysql server Quote Link to comment https://forums.phpfreaks.com/topic/239638-cron-job-mysql-in-php/#findComment-1231180 Share on other sites More sharing options...
drmota Posted June 18, 2011 Author Share Posted June 18, 2011 They are on the same pc, correct folder. Works from browser but not terminal Why does it have to be so hard.. Quote Link to comment https://forums.phpfreaks.com/topic/239638-cron-job-mysql-in-php/#findComment-1231427 Share on other sites More sharing options...
gristoi Posted June 18, 2011 Share Posted June 18, 2011 Let's back track a bit, is this all you have in the file your testing? : <?php #!/usr/bin/php mysql_connect('localhost','usrname','pw') or die ('Error connecting'); ?> And if so what made u sure it was working in the browser ? Quote Link to comment https://forums.phpfreaks.com/topic/239638-cron-job-mysql-in-php/#findComment-1231443 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.