我的服务器上有一个cron.php文件,该文件与数据库建立连接并执行某些功能。当我使用浏览器执行此文件时,它运行完美。但是当它从cron执行时,它会在进行数据库连接时出现以下错误:
ERROR: SQLSTATE[28000] [1045] Access denied for user 'USER_NAME'@'localhost' (using password: YES)<br />
我搜索了很多,但不知道该怎么做。有什么想法吗?
编辑:基本上,这是我的cron.php:
<?php
require_once('phpInclude/db_connection.php');
error_reporting(1);
$sql_qry = "select email, apn_id, reg_id, token from users where verify = 'n' and DATEDIFF(NOW(),created_on) = 3";
$res=$con->query($sql_qry);
?>
我的db_connection.php:
<?php
require_once('config.php');
try {
$dsn = 'mysql:host='.$DB_HOST.';dbname='.$DB_DATABASE;
$con = new PDO($dsn, $DB_USER, $DB_PASSWORD);
$con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $e) {
echo 'ERROR: ' . $e->getMessage();
}
$DB_HOST = LOCALHOST;
$DB_DATABASE = DB_NAME;
$DB_USER = USER_NAME;
$DB_PASSWORD = USER_PASS;
?>
我的config.php:
define('LOCALHOST','localhost');
define('USER_NAME','xxxxxxxxxxx');
define('USER_PASS','xxxxxxxxxx');
define('DB_NAME','xxxxxxxxxx');
define("UPLOAD_PATH","xxxxxxxxxxxx");
define("BASE_PATH","xxxxxxxxxxxxxx");
答案 0 :(得分:0)
CRON通常从CLI运行,因此它可能不会像浏览cron.php时那样从webroot访问此文件。尝试将require更改为绝对路径:
require_once __DIR__ . 'phpInclude/db_connection.php';
答案 1 :(得分:0)
问题解决了。我在config.php文件中进行了localhost检查。起初,我使用wamp在localhost上创建了webservices,因此配置是localhost。后来,我在我的服务器上移动了项目。我通过添加:
来更改配置if(server="http://myserver.com")
//take server config
else
//take localhost config
cron服务选择了else else子句中的config。