Jump to content

Recommended Posts

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 :)

Link to comment
https://forums.phpfreaks.com/topic/239638-cron-job-mysql-in-php/
Share on other sites

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

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

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! :)

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 :)

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

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 :)

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.