No connection to database in xampp 7.1.1

Here’s my code for test_connection.php:

$pdo = new PDO('mysql:host=localhost;dbname=clear', 'institute', 'E7@!\/');
$pdo->exec('SET NAMES "UTF8"');
ECHO 'Success!';
catch (PDOException $e)
echo 'Unable to connect to the database server.';

The database ‘clear’ has been setup with the user name ‘institute’ and password.
Is there any adjustment to be made to the settings? Thanks for your response.

We cannot know.
You should ask not us but your server.

Remove that useless and harmful try…catch stuff forever and you will see the actual error message that you’ll be able to read, comprehend and fix.

Fatal error: Uncaught exception ‘PDOException’ with message ‘SQLSTATE[HY000] [1045] Access denied for user ‘institute’@‘localhost’ (using password: YES)’ in C:\xampp\htdocs\test_connection.php:3 Stack trace: #0 C:\xampp\htdocs\test_connection.php(3): PDO->__construct(‘mysql:host=loca…’, ‘institute’, ‘E7@!\/’) #1 {main} thrown in C:\xampp\htdocs\test_connection.php on line 3

So the problem is evident now.
As a possible solution try to make your password less fancy. If not helped double check all other credentials.

@colshrapnel: I changed the password to ‘great’ and the connection to the database is now established. I had earlier changed, without any effect, the Server connection collation from utfmb4_unicode_ci to utf8_general_ci (though the system changed it to utf8mb4_general_ci). Thank you so much for your assistance.

I wonder whether the presence of a backslash character inside the original password would cause PHP to implement it’s escaping rules, as it would in any other string, leading to the rejection.

I suppose it’s not php but rather mysql.

Never thought of the backslash in the password until now.

I don’t know if PHP would see the backslash as a literal or as an escape. But it looks like depending on the NO_BACKSLASH_ESCAPES mode it’s ignored

Within a string, certain sequences have special meaning unless the NO_BACKSLASH_ESCAPES SQL mode is enabled. Each of these sequences begins with a backslash (), known as the escape character. MySQL recognizes the escape sequences shown in Table 10.1, “Special Character Escape Sequences”. For all other escape sequences, backslash is ignored. That is, the escaped character is interpreted as if it was not escaped. For example, \x is just x.

I’d say NO_BACKSLASH_ESCAPES is rather irrelevant here as nobody in their right mind would switch it on.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.