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);
?>
Related
I have a website hosted by united domains. It was working fine with MySQLi but I changed it to PDO. Now I cannot get the database to work. I've tried changing $host to 127.0.0.1, and to localhost. If I use localhost, I get the error SQLSTATE[HY000] [2002] No such file or directory.
If I use 127.0.0.1 I get SQLSTATE[HY000] [2002] Connection refused.
I have also tried by changing collation but it still doesn't work. It works perfectly fine on my other domain that is on Hostinger.
$host = "localhost"; // I have tried using both 127.0.0.1 and localhost here
$db = "db_name"; //db name goes here
$username = "username"; //username goes here
$password = "password"; //password goes
try {
//Creating a PDO instance
$conn = new PDO("mysql:host=$host;dbname=$db", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$conn->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$conn->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ);
}catch(\PDOException $e){
// if connection fails, show PDO error
echo "Connection Failed: ". $e->getMessage();
}
I am new to both php and aws and I have come across an issue trying to link the aws to the simple-login php script.
(https://github.com/panique/php-login-minimal).
I'm getting the following error printed:
SQLSTATE[HY000] [2002] Connection refused
This is my code
<?php
ob_start();
session_start();
//set timezone
date_default_timezone_set('Europe/London');
//database credentials
define('DBHOST','instancename.cfci0i1rm4rl.us-east-2.rds.amazonaws.com');
define('DBUSER','name_root');
define('DBPASS','rootpassword');
define('DBNAME','login');
//application address
define('DIR','https://union.washmel.com/user/login/auth/lr/');
define('SITEEMAIL','noreply#domain.com');
try {
//create PDO connection
$db = new PDO("mysql:host=".DBHOST.";charset=utf8mb4;dbname=".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);
?>
as follows is the link to the error on my site as it currently stands.
https://union.washmel.com/user/login/auth/lr/index
I am learning MySQL/PHP and I cannot figure out how to connect to MySQL on my localhost. I have written a short bit of code and I included root as my user name and root as my password because I have not set these elements yet (as far as I know). I feel that perhaps I am missing something in regards to the username/password combination. However, I feel that this should not be an issue because I have not tampered with the default conditions.
I need some help.
My code is below:
<?php
try {
$db = new PDO("mysql:host=localhost;dbname=shirts4max;port=3306", "root", "root");
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$db->exec("SET NAMES 'utf-8'");
} catch (Exception $e) {
echo "Could not connect to the database.";var_dump($e);
exit;
}
I am only seeing the error message on my page:
"Could not connect to the database."
Thank for reading. Please help me Obiwan.
Show the real error. Avoid using root except for maintenance.
From the PDO Manual page here, modified for you
<?php
$dsn = 'mysql:dbname=shirts4max;host=localhost';
$user = 'root';
$password = 'root';
try {
$dbh = new PDO($dsn, $user, $password);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
}
?>
Have you tried connecting via command line using: mysql -uroot -proot -hlocalhost -p3306? If you can't connect that way, the PDO connection won't go through either. Playing with the connection info via command line gives you a nice easy way to find what works, then include it in your code. Also, if you've not set the user/pw, it's possible there isn't one currently, so you wouldn't need those parameters at all.
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
So I've just set up an old script of mine which works fine on all the web hosts I've tried but one (including localhost).
Anyhow, I've set up the database and it's connected (I've tried the connection and it should work fine), though I am recieving an error that my site can not connect to the database.
I am receiving the PDOException message no matter what.
config.php
<?php
require "details.php";
try {
$dbh = new PDO("mysql:host=$hostname;dbname=$database", $username, $password);
}
catch(PDOException $e)
{
echo 'Your website can not connect to the database';
}
details.php
<?php
$hostname = 'localhost';
$username = 'myuser';
$password = 'mypassword';
$database = 'mydatabase';
define('DB_NAME', 'mydatabase');
define('DB_USER', 'myuser');
define('DB_PASS', 'mypassord');
define('DB_HOST', 'localhost');
I've come to the conclusion that it must be one of those codes that it must be something to do with the above code.
I have tried this code below to make sure the PDO extention is enabled and yes it is.
<?php
if (!defined('PDO::ATTR_DRIVER_NAME')) {
echo 'PDO unavailable';
}
elseif (defined('PDO::ATTR_DRIVER_NAME')) {
echo 'PDO available';
}
Any idea, maybe why?
Get rid of that catch stuff.
Leave only three lines in this file
<?php
require "details.php";
$dbh = new PDO("mysql:host=$hostname;dbname=$database", $username, $password);
and see what does it say, either on screen or in log, according to site settings.