2

I'm following this tutorial, I'm currently around minute 04:00 and I want to make a connection with my MySQL database through PDO. But my webpage will always give "Could not connect." when I'm trying to make the connection. When I used PHPStorms inside Database program, I had to change my serverTimezone to Europe/Amsterdam and then I was able to connect to my db.

I tried to add the port number in the 'new PDO()' code. I tried to change the timezone in the code and on my MySQL server but it gives this error;

mysql> SET GLOBAL time_zone = 'Europe/Amsterdam';
ERROR 1298 (HY000): Unknown or incorrect time zone: 'Europe/Amsterdam'
<?php

try {
    $pdo = new PDO('mysql:host=localhost:dbname=mytodo', 'root', '');
} catch (PDOException $e) {
    die('Could not connect.');
}

$statement = $pdo->prepare('select * from todos');

$statement->execute();

var_dump($statement->fetchAll());

require 'index.view.php';

Extra information:

mysql> SELECT @@global.time_zone, @@session.time_zone;
+--------------------+---------------------+
| @@global.time_zone | @@session.time_zone |
+--------------------+---------------------+
| SYSTEM             | SYSTEM              |
+--------------------+---------------------+
  • `mysql> SET GLOBAL time_zone = 'Europe/Amsterdam';` is not PHP, nor PDO. Please clarify the question. Is it a SQL query or PDO connection? – user3783243 Oct 26 '19 at 04:43
  • Okay, so I used the code of 'your common sense', I restarted my mysql services and then got this error: Fatal error: Uncaught PDOException: SQLSTATE[HY000] [2054] The server requested authentication method unknown to the client You can fix that with: ALTER USER 'YOUR_USER_NAME'@'localhost' IDENTIFIED WITH mysql_native_password BY 'ENTER_YOUR_PASSWORD_HERE'; –  Oct 26 '19 at 13:52
  • `ALTER USER 'YOUR_USER_NAME'` is not in this question.. also what are you attempting to do with that statement? – user3783243 Oct 26 '19 at 14:33

2 Answers2

2

The tutorial you are using is a bit outdated. And the essential part is

die('Could not connect.');

It's a nasty statement that, sadly, is wandering from one outdated tutorial to another. I even had to write a dedicated answer on Stack Overflow explaining why it is bad. The thing is, PDO already has an answer why you couldn't connect. But this code effectively muting it out.

So the question is, how to properly connect with PDO, the way it will tell you what is certainly going wrong when something going wrong? And here goes my own tutorial entirely dedicated to the question How to connect to MySQL with PDO.

$host = '127.0.0.1';
$db   = 'mytodo';
$user = 'root';
$pass = '';
$charset = 'utf8mb4';

$options = [
    \PDO::ATTR_ERRMODE            => \PDO::ERRMODE_EXCEPTION,
    \PDO::ATTR_DEFAULT_FETCH_MODE => \PDO::FETCH_ASSOC,
    \PDO::ATTR_EMULATE_PREPARES   => false,
];
$dsn = "mysql:host=$host;dbname=$db;charset=$charset";
try {
     $pdo = new \PDO($dsn, $user, $pass, $options);
} catch (\PDOException $e) {
     throw new \PDOException($e->getMessage(), (int)$e->getCode());
}

Here you can see the proper connection code that does a lot of things that are just missed in your tutorial. Among them, it tells you what the actual problem is.

So you have to change your code and run it again. Then it will display you the actual error message. Then you will have to either read and fix it right away or, Google for the message you get and find a solution here on Stack Overflow.

Your Common Sense
  • 154,967
  • 38
  • 205
  • 325
  • Fatal error: Uncaught PDOException: SQLSTATE[HY000] [2054] The server requested authentication method unknown to the client –  Oct 26 '19 at 13:46
  • Yes! Now you know the problem. And the solution for this particular problem is easily Google-able, just like I said: this is a [well known problem](https://stackoverflow.com/questions/52364415/php-with-mysql-8-0-error-the-server-requested-authentication-method-unknown-to). Just remember - the problem could be anything and the key is to get the actual error message. The rest is simple – Your Common Sense Oct 26 '19 at 13:50
  • Okay, so I used the code of 'your common sense', I restarted my mysql services and then got this error: Fatal error: Uncaught PDOException: SQLSTATE[HY000] [2054] The server requested authentication method unknown to the client. You can fix that with: ALTER USER 'YOUR_USER_NAME'@'localhost' IDENTIFIED WITH mysql_native_password BY 'ENTER_YOUR_PASSWORD_HERE'; –  Oct 27 '19 at 14:04
-1

You have error in code near localhost

Use semicolon in place of colon

Write this instead:

$pdo=new PDO('mysql:host=localhost;dbname=mytodo','root','');
Dharman
  • 26,923
  • 21
  • 73
  • 125