My script has stopped connecting to a database

Codes here !

Moderators: egami, macek, gesf

Post Reply
IChaps
New php-forum User
New php-forum User
Posts: 106
Joined: Tue Mar 01, 2016 3:07 pm

Sun Oct 01, 2017 9:33 am

Hello.

Could I please just enquire.
I have recently moved my site to a new server. I've created a new database and user, then changed the username, password and database name in my script to match the database.

However I'm now finding I'm getting the error message:- Connection failed: SQLSTATE[HY000] [1045] Access denied for user reguser'@'localhost' (using password: YES)

I've started re-writing my code:-

Code: Select all

$servername =  "localhost";
$dbname =  "members";
$username =  "reguser";
$password =  "qwerty"; // Simulated password for this query

try {
    $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
    // set the PDO error mode to exception
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    echo "Connected successfully";
    }
catch(PDOException $e)
    {
    echo "Connection failed: " . $e->getMessage();
    }
I would like to ask if the connection method for database has changed, and if not, any suggestion on how to get connected?

Thank You very much.

User avatar
Strider64
php-forum Active User
php-forum Active User
Posts: 310
Joined: Sat Mar 23, 2013 8:24 am

Sun Oct 01, 2017 10:30 am

Looks like you `might` be using a local connection instead of the remote server credentials?

What I do is this in a configuration file:

Code: Select all

if (filter_input(INPUT_SERVER, 'SERVER_NAME', FILTER_SANITIZE_URL) == "localhost") {
    define('DATABASE_HOST', 'localhost'); // usually localhost
    define('DATABASE_NAME', 'cms');
    define('DATABASE_USERNAME', 'username');
    define('DATABASE_PASSWORD', 'password');
    define('DATABASE_TABLE', 'users');
} else {
    define('DATABASE_HOST', 'remote_database_host');
    define('DATABASE_NAME', 'remote_db_name');
    define('DATABASE_USERNAME', 'remote_username');
    define('DATABASE_PASSWORD', 'remote_password');
    define('DATABASE_TABLE', 'users');
}
That way I can just do the following:

Code: Select all

        $db_options = array(
            /* important! use actual prepared statements (default: emulate prepared statements) */
            PDO::ATTR_EMULATE_PREPARES => false
            /* throw exceptions on errors (default: stay silent) */
            , PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
            /* fetch associative arrays (default: mixed arrays)    */
            , PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
        );
        $conn = new PDO('mysql:host=' . DATABASE_HOST . ';dbname=' . DATABASE_NAME . ';charset=utf8', DATABASE_USERNAME, DATABASE_PASSWORD, $db_options);
Once I get the website done I just get rid of the if statement and keep the remote settings. This way I don't have think (or change) the connections settings for the database.
Life is a fig newton of your imagination! https://www.pepster.com/index

IChaps
New php-forum User
New php-forum User
Posts: 106
Joined: Tue Mar 01, 2016 3:07 pm

Mon Oct 02, 2017 4:28 am

Hello Strider64,

Thank you for your post.

Kind Regards.

Post Reply