I have a cron.php file on my server which makes connection with the database and do some function. When I execute this file using browser, it runs perfectly. But when it executes from cron, it gives following error while making database connection:
ERROR: SQLSTATE[28000] [1045] Access denied for user 'USER_NAME'#'localhost' (using password: YES)<br />
I searched a lot but no clue what to do. Any ideas?
EDIT: Basically, this is my 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);
?>
My 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;
?>
My 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");
Problem solved. I was having a localhost check in my config.php file. At first, I made the webservices at localhost using wamp, so the config was of localhost. Later, I moved the project on my server. And I changed the config by adding:
if(server="http://myserver.com")
//take server config
else
//take localhost config
And cron service picked the config inside else clause.
CRONs typically run from the CLI, so it may not be accessing this file from the webroot like when you browse to cron.php. Try changing the require to an absolute path:
require_once __DIR__ . 'phpInclude/db_connection.php';
Related
I am relatively new to php and have been following a tutorial for a beginner project. This project includes a database using the DBO object with mysql. I am using MAMP to run php. When I try to run my project it gives me the error in the title. Here is my code for database setup:
<?php
$dsn = 'mysql:host=localhost;dbname=assignment_tracker';
$username = 'root';
try {
$db = new PDO($dsn, $username);
} catch (PDOException $e) {
$error = "Database Error: ";
$error .= $e->getMessage();
include('view/error.php');
exit();
}
I am able to enter the phpmyadmin page. Looking at the user table in the mysql database shows this:
My port in the php.ini file is 8889. (My apache port is 8888 but my mysql is 8889 not sure which one it should be set to.) Looking at other answers on this website have been really confusing for me so any help would be greatly appreciated.
Try adding mysql's port to the $dns and add the password (if it's not null) as part of the connection. Try this
<?php
$dsn = 'mysql:host=127.0.0.1;port=8889;dbname=assignment_tracker';
$username = 'root';
$password = '';
try{
$db = new PDO($dsn, $username, $password);
}catch (PDOException $e){
$error = "Database Error: ";
$error .= $e->getMessage();
include('view/error.php');
exit();
}
I researched and found out oci_connect() is the way to go. I found out that i could either use the Connect Name from the tnsnames.ora file or use an easy connect syntax. Since my database isn't locally stored and I had no idea where the said, tnsnames.ora file was located in apex.oracle.com, I went with easy connect strings.Here's what I've done so far.
$username = "myemail";
$host = "apex.oracle.com";
$dbname = "name";
$password = "password";
// url = username#host/db_name
$dburl = $username . "#".$host."/".$dbname;
$conn = oci_connect ($username, $password, $dburl);
if(!$conn) echo "Connection failed";
I get a
Call to undefined function oci_connect()
So what would be the way to go?
UPDATE 1:
Here's the list of things I did:
Installed Oracle DB
Unzipped Oracle Instance client
Set the environment variables
Uncommented the extension=php_oci8_12c.dll in php.ini
Copied all the *.dll files from the instance client folder to xampp/php and xampp/apache/bin
also made sure the php/ext folder had the required dlls.
That was last night. I have restarted my PC multiple times, APACHE with it but I'm still getting this error:
Call to undefined function oci_connect()
At this point I'm frustrated and don't know where to go from here. PHP just doesn't seem to link up to oci8. I can view the databases I made from Database Configuration Assistant in cmd from 'sqlplus' command and a few select statements. So everything seems to be setup right, its just the php that's having problems trying to use oci_connect().
My database.php, now is setup as:
public function __construct()
{
error_reporting(E_ALL);
if (function_exists("oci_connect")) {
echo "oci_connect found\n";
} else {
echo "oci_connect not found\n";
exit;
}
$host = 'localhost';
$port = '1521';
// Oracle service name (instance)
$db_name = 'haatbazaar';
$db_username = "SYSTEM";
$db_password = "root";
$tns = "(DESCRIPTION =
(CONNECT_TIMEOUT=3)(RETRY_COUNT=0)
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP)(HOST = $host)(PORT = $port))
)
(CONNECT_DATA =
(SERVICE_NAME = $db_name)
)
)";
$tns = "$host:$port/$db_name";
try {
$conn = oci_connect($db_username, $db_password, $tns);
if (!$conn) {
$e = oci_error();
throw new Exception($e['message']);
}
echo "Connection OK\n";
$stid = oci_parse($conn, 'SELECT * FROM ALL_TABLES');
if (!$stid) {
$e = oci_error($conn);
throw new Exception($e['message']);
}
// Perform the logic of the query
$r = oci_execute($stid);
if (!$r) {
$e = oci_error($stid);
throw new Exception($e['message']);
}
// Fetch the results of the query
while ($row = oci_fetch_array($stid, OCI_ASSOC + OCI_RETURN_NULLS)) {
$row = array_change_key_case($row, CASE_LOWER);
print_r($row);
break;
}
// Close statement
oci_free_statement($stid);
// Disconnect
oci_close($conn);
}
catch (Exception $e) {
print_r($e);
}
}
And it outputs:
oci_connect not found
OCI8 is listed in my phpInfo().
Okay I found out the culprit behind this whole ordeal. I had set the PATH Environment Variables but apparently forgot to add a new system environment variable named TNS_ADMIN and set the directory to PATH/TO/INSTANCE/CLIENT.
Here's the list of System Environment variable you need to add:
Edit PATH system variable and add the $ORACLE_HOME/bin dir
Edit PATH system variable and add the Instance Client dir
Add new system variable, name it TNS_ADMIN and add the Instance Client dir
I hope this helps those who come looking.
First, this has been asked before, but Oracle doesn't allow remote database connections to their free apex.oracle.com example service. Sorry. You can only interact with it through the web interface.
Second, if you do find a remote Oracle db to connect to, you'll need to install the Oracle Instant Client for your OS, and configure the PHP OCI8 extension.
I am much confuse about 000webhost database uploading I have set my database at their and accessing it with username and password perfectly. Now trying to connect with through PHP code but i am failed, getting 500 error code and no details of the error although I have also logged exception message in my connection code nuto it only showing "A Connection error has occurred" and nothing more:
//DB Configuration
$hostName = 'localhost';
$dbUserName = 'id143984_jw_kioskuser';
$dbPassword = '***********';
$databaseName = 'id143984_jw_kiosk';
try{
$dbHandler = new PDO('mysql:host='. $hostName . ';dbname='. $databaseName, $dbUserName, $dbPassword);
}
catch(PDOException $ex){
echo '<h1>A Connection error has occurred.</h1><pre>', $e->getMessage() ,'</pre>';
echo $e->getMessage();
}
My current DB interface at 000webhost saying me that "Use localhost as connection hostname"
while in another post of 000WEbhost saying "Never use 'localhost' as your MySQL hostname! You can find your MySQL hostname by logging on to members area, entering control panel and clicking on MySQL icon."
HOw do i make successfull connection?
Finally i managed to get the answer through mysql query thanks to Show MySQL host via SQL Command Answer at stack
I run below query and got the host name and use it in my php it worked now.
select ##hostname;
show variables where Variable_name like '%host%';
$hostName = 'mysql6.000webhosting.com';
$dbUserName = 'id143984_jw_kioskuser';
$dbPassword = '***********';
$databaseName = 'id143984_jw_kiosk';
$con = mysql_connect($hostName,$dbUserName,$dbPassword);
if (!$con)
{
die('Could not connect: ' . mysql_error());
}`enter code here`
mysql_select_db("$databaseName",$con);
this should give you the real reason and note the correct host name
so i'm having my database connection in my project. The file has a local and an online request, one is commented out when i work on localhost.
is there a way in php for it to look up which one it has to use?
like if url = localhost/blabla use local else use online?
Here is the connection.
try{
//local connection
$db = new PDO('mysql:host = 127.0.0.1; dbname=XXXX', 'root','');
//local online
//$db = new PDO('mysql:host = XXXXX; dbname=XXXXX', 'xxxxx','*****');
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}catch(PDOException $e){
echo $e->getMessage();
die();
}
$db->exec("set names utf8");
if (session_status() == PHP_SESSION_NONE) {
session_start();
}
Hope somone can help if it's possible, thanks
You shouldn't check for the URL but setup a config file. Then you set DEVELOPMENT on 0 on the server, and on 1 if you are local.
config.php
<?php
define('DEVELOPMENT', 1);
?>
database.php
<?php
if(DEVELOPMENT) {
// setup your local DB
} else {
// setup your live db
}
?>
You can know the IP through the $_SERVER variable,so:
define('LOCALHOST', 'XX.XX.XX.XX');
if($_SERVER["REMOTE_ADDR"] == LOCALHOST)
{
//open connection to the localhost users db
$db = new PDO('mysql:host = 127.0.0.1; dbname=XXXX', 'root','');
}else{
//open connection to rest of users db
$db = new PDO('whatever', 'root','');
}
Do this, put your db.php in it's own folder, after uploading it to the server, then change the user rights so you can't overwrite it with your local connection
I am moving what looks like a pretty simple php/MSQL app from a Windows IIS server to a Cpanel/ Apache server. The app works fine on existing IIS/MySQL server using this format to connect:
<?php
class Connection {
var $conn;
var $user = 'XX0000XX_XXadmXX';
var $pass = 'XXXpasswordXXXX';
var $dbname = 'XXckbXXX_XXX13XX';
var $host = '74.0.0.0';
function Connection() {
$this->conn = mysql_connect($this->host, $this->user, $this->pass);
mysql_select_db($this->dbname,$this->conn) or die( "db-connect". mysql_error());
}
function closedb() {
//mysql_close(($this->host, $this->user, $this->pass ) or die( "db-close". mysql_error() );
mysql_close($this->conn)or die( "db-close". mysql_error() );
}
}
?>
I imported the DB into CPANEL/MySQL using PhpMyAdmin. 1st test failed due to $host not accepting IP address value. After changing IP to localhost - tested the connection with a DB connection script and that passed. The error is that at I can connect with the same username and PW as Windows server. But after logging in any link I click in the admin kicks me out.
Any clues would be helpful - let me know if I can provide any information that would help. I am thinking that there may be a windows specific change that needs to happen or something to do with the the DB host.
Here are some values in a settings file - before the Ip addresses were set with domain name - set with IP to test:
define('ADMIN_MANAGEMENT', "sitename");
define('FROM_NAME', "Admin");
define('SITE', "sitename");
define('SITE_NAME', "http://74.0.0.0/");
define('ADMIN_ID', "dwayne#fair.com");
define('KEYWORD', "sitename");
define('SITE_URL',"http://74.0.0.0/");
define('SITEIMGPATH', "http://74.0.0.0/public/images/");
define('ITEMS_PER_PAGE', "15");
define('PRODUCTS_PER_PAGE', "5");
define('UPLOAD_FILES', "uploads/files/");
define('UPLOAD_IMAGES', "uploads/images/");
define('SMALL_IMAGE_WIDTH',190);
define('SMALL_IMAGE_HEIGHT',77);
define('PROFILENAME', "Welcome to domain.com");
define('CARID',1000);
define('RESIZE_IMAGE_WIDTH',400);
define('RESIZE_IMAGE_HEIGHT',300);
/*Live path*/
define("ROOT", "http://" . $_SERVER["SERVER_NAME"] . "/admin/");
define("ROOTS", "http://" . $_SERVER["SERVER_NAME"] . "/admin/");