简体   繁体   中英

Cannot connect to MySQL Database with Cron job

I'm trying to make a cron job to delete any users that have deactivated their accounts a year ago, the script works on the website but when doing it through a cron job, I get a fatal error.

Fatal error: Cannot instantiate non-existent class: pdo in /homepages/9/d526231279/htdocs/cupidmemories/cronjobs/users.cron.php on line 2

Here is my users.cron.php file:

<?php
$db = new PDO('mysql:host=dbhost.com:port; dbname=dbname;', 'user', 'pass');

$query = "DELETE FROM users WHERE account_deleted='4'";
$db->query($query);
?>

Most Apache/PHP installations have 2 seperate php.ini files.

I would say that the one used by Apache/PHP is fine, but the other one does not have the PDO extension activated.

Do a

>php --ini 

from the command line and you will see line which tells you where PHP CLI got its php.ini file from. Edit that so that the

extension=php_pdo_mysql.so

is activated

The Fatal error: Cannot instantiate non-existent class error message was shown by good old PHP/4 . The PDO class requires PHP/5.1.0 or greater. You simply cannot use such an outdated interpreter to run anything that resembles modern code.

Hopefully, your hosting provider might have a newer interpreter available if you provide the appropriate full path, so you could replace this:

*/10 * * * * php /homepages/9/d526231279/htdocs/cupidmemories/cronjobs/users.cron.php

... with eg this:

*/10 * * * * /opt/php5.6/bin/php /homepages/9/d526231279/htdocs/cupidmemories/cronjobs/users.cron.php

(These are fake paths, don't just copy them expecting to work.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM