(SOLVED) PHP PDO: Connection to MySQL Database with a password that has special characters?

<?php

// MySQL Connection information variables.
$_db_PDO_Conn = "";
$_db_Server_Name = 'localhost';
$_db_Username = 'db_user';
$_db_Password = 'h2=^r3m!1-dp';
$_db_Name = 'test';

try
{ // Create connection to MySQL Database.
	$_db_PDO_Conn = new PDO
		( // Check connection to MySQL Database.
		"mysql:host=$_db_Server_Name;dbname=$_db_DB_Name",
		$_db_Username,
		$_db_Password
		);
	$_db_PDO_Conn->setAttribute
		( // Prepare Attribute for Error Mode Exception Handling.
		PDO::ATTR_ERRMODE,
		PDO::ERRMODE_EXCEPTION
		);
}
catch(PDOException $_e)
{ // If error occurs, Exception causes automatic transaction rollback...
	$_db_PDO_Conn = null; // Null connection to the database...
//	mail_errors($_e->getMessage()); // Mail errors accordingly.
	print_r ($_e->getMessage());
}

$_db_PDO_Conn = null; // Null connection variable closes connection.

?>

When run in Powershell with the following command:

php.exe C:\PHP\test.php

I get this:

SQLSTATE[HY000] [1045] Access denied for user 'db_user'@'localhost' (using password: YES)

If I change the password to something simple (say, waffles), then try it, it works fine.

I assume it's the special characters causing the issue. I am not sure how to effectively escape them for MySQL.

Any help is appreciated.

have a INI file with the user name and password, then parse the ini file.

2 Likes

That worked wonderfully. Thanks.

Example .ini:

; This is a sample configuration file
; Comments start with ';', as in php.ini

[first_section]
username = db_user
password = 'passwordwith#(*)%' ; Note the ' are necessary if you have an = in the password.

Example code:

<?php

// MySQL Connection information variables.
$_db_PDO_Conn = "";
$_db_Server_Name = 'localhost';
$_db_Creds = parse_ini_file("db_creds.ini");
$_db_Name = 'test';

try
{ // Create connection to MySQL Database.
	$_db_PDO_Conn = new PDO
		( // Check connection to db MySQL Database.
		"mysql:host=$_db_Server_Name;dbname=$_db_DB_Name",
		$_db_Creds['username'],
		$_db_Creds['password']
		);
	$_db_PDO_Conn->setAttribute
		( // Prepare Attribute for Error Mode Exception Handling.
		PDO::ATTR_ERRMODE,
		PDO::ERRMODE_EXCEPTION
		);
}
catch(PDOException $_e)
{ // If error occurs, Exception causes automatic transaction rollback...
	$_db_PDO_Conn = null; // Null connection to the database...
//	mail_errors($_e->getMessage()); // Mail errors accordingly.
	print_r ($_e->getMessage());
}

$_db_PDO_Conn = null; // Null connection variable closes connection.

?>
3 Likes

I would also store the connection information as well, as its safer than just keeping the info in the class files as long as it is placed outside the document root.

#PHP
$_db = parse_ini_file("config.ini");
$_db_Server = $_db['server'];
$_db_Name = $_db['database'];
$_db_User = $_db['username'];
$_db_Pass = $_db['password'];

#config.ini
[info]
;database connection info
username = db_user
password = the_password
server   = localhost
database = test

Also, this allows you to change connection information at anytime, so that you won't have to change the actual PDO code every time the database info changes. This also makes it multi-use so you could use this anywhere.

2 Likes