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
Related
I tried different ports and restarting service mysql. All answerers that I found where for localhost and didn't helped me. Also tried different time zones thought it might be a problem. Don't know tho what to put under 'application address' maybe that is the problem. Thank you for answer.
<?php
ob_start();
session_start();
//set timezone
date_default_timezone_set('Europe/Amsterdam');
//database credentials
define('DBHOST','servers_ip');
define('DBUSER','root');
define('DBPASS','pass');
define('DBNAME','dbname');
//application address
define('DIR','https://domain/test/');//includes
define('SITEEMAIL','noreply#domain.com');//i leave it like this
try {
//create PDO connection
//
$db = new PDO("mysql:host=".DBHOST.";port=8889;dbname=AppDatabase".DBNAME, DBUSER, DBPASS);
//
//$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_SILENT);//Suggested to uncomment on production websites
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);//Suggested to comment on production websites
$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
} catch(PDOException $e) {
//show error
echo '<p class="bg-danger">'.$e->getMessage().'</p>';
exit;
}
//include the user class, pass in the database connection
include('classes/user.php');
include('classes/phpmailer/mail.php');
$user = new User($db);
?>
im having a problem, my project is working in my windowsOS, and i try it to transfer in Ubuntu16.04 Operating system, with static ip address. now when i trying to access the project it keep saying Database Connection Error!
<?php
$db_password = "Welcome1";
$server = "192.168.2.218";
try{
$pdo_conn = new
PDO("mysql:host=$server;dbname=boldr","phpmyadmin","
{$db_password}");
} catch (PDOException $e){
die("Database Connection Error!");
}
$conn = mysqli_connect("192.168.2.218","phpmyadmin","{
$db_password}") or die(mysqli_error($conn));
mysqli_select_db($conn,'boldr') or die("cannot select DB");
?>
is your database on the same server? If yes try 127.0.0.1 or localhost.
I am developing a dynamic website and I'm using both local and remote host. I've tried to implement a code in which I stablish connection first with the localhost. If connection to the locahost is false or can't be established, I go to the second connection (remote server or host). But this time, I want to do something different. Now I want to know if it's possible to connect to two hosts/servers at the same time with no errors. The databases and tables are the same. I'm asking this, because I just want to avoid sql backup. What do you say? Moreover, I want to use the same variable $pdo, so I don't need to change or repeat all connections to the tables.
This is my current code:
<?php
try {
//Local Host - XAMPP
$dsn = 'mysql:host=localhost;dbname=bananas';
$user = 'root';
$pw = '';
$sessionpath = 'C:/xampp/tmp';
$pdo = new PDO($dsn, $user, $pw);
$pdo->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e) { //$e
//echo 'Error: '.$e->getMessage();
if(!isset($pdo) || $pdo == false){
try{
//Remote Host
$dsn = 'mysql:host=bananas123;dbname=bananas';
$user = 'mybigbanana';
$pw = '6969';
$sessionpath = '/php_sessions';
$pdo = new PDO($dsn, $user, $pw);
$pdo->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
}catch(PDOException $e) {
echo 'Error: '.$e->getMessage();
}
}
}
?>
Note: I don't want to do something like having two variables for each connection, like, for example, $pdo and $pdo2. I want only one variable for both connections, which is $pdo.
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';
I'm trying to connect to my mySQL database using the PDO class in PHP.
Here is my Code :
// Connects to Our Database via PDO.
if($local) {
try {
$db = new PDO("mysql:=localhost;dbname=bbc_archive;port=3306", "root", "");
$db->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
$db->exec("SET NAMES 'utf8'");
} catch(Exception $e) {
echo "Connection to the DataBase was not possible. ";
die();
}
} else {
try {
$db = new PDO("mysql:=bbcarchive.db.11505263.hostedresource.com;dbname=bbcarchive", "bbcarchive", "myPassword");
$db->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
$db->exec("SET NAMES 'utf8'");
} catch(Exception $e) {
echo "Connection to the live DataBase was not possible. ";
die();
}
}
The $local variable is defined before and determines whether or not the script is running on a the live server or a test server.
When running in my local environment everthing works fine but on my live server it echo's out "Connection to the live DataBase was not possible." from the catch block.
I've contacted my host provider (godaddy) and they think it's a coding error. I've also, obviously, checked the hostname, dbname, username and password a 100 times and it's all correct. I just can't see the problem!
How can i do this ?
Your DSN seems to be incorrect. The documentation on MySQL DSNs indicates that it should look somewhat like this:
$db = new PDO("mysql:host=localhost;dbname=bbc_archive;port=3306", "root", "");