【发布时间】:2015-02-15 00:04:15
【问题描述】:
我的服务器上有一个 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");
【问题讨论】:
-
无。我们不知道您的数据库用户名和/或密码是什么。
-
我认为您的密码不匹配..再检查一遍
-
@MarcB 恕我直言,我只是在询问我收到此错误的情况。
-
就是这样。您没有使用正确的凭据。错误的用户名、错误的密码和/或错误的主机名。
-
@Harshana 当我使用它的完整 url 从浏览器执行这个 php 文件时它可以工作。