F
F
fomenko_alexandr2016-10-08 19:54:12
PHP
fomenko_alexandr, 2016-10-08 19:54:12

Why is pdo not working?

Hello. I started studying OOP, and at the same time, pdo.
I'm trying to write a database connection class, but an error occurs.
Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[28000] [1045] Access denied for user 'root'@'localhost' (using password: YES)' in C:\OpenServer\domains\oop\includes\db_connects. php:16 Stack trace: #0 C:\OpenServer\domains\oop\includes\db_connects.php(16): PDO->__construct('mysql:host=loca...', 'root', '111111', Array) #1 C:\OpenServer\domains\oop\public\index.php(7): MySQLDatabase->__construct() #2 {main} thrown in C:\OpenServer\domains\oop\includes\db_connects.php on line 16

<?php

include 'config.php';
//содержимое config.php
//defined('DB_SERVER')  ? null : define('DB_SERVER', 'localhost');
//defined('DB_USER')    ? null : define('DB_USER', 'root');
//defined('DB_PASS')    ? null : define('DB_PASS', '111111');
//defined('DB_NAME')    ? null : define('DB_NAME', 'oop');

class MySQLDatabase {
    private $dsn;
    private $opt = array();
    private $db;

    function __construct() {
        $this->dsn = "mysql:host=".DB_SERVER.";dbname=".DB_NAME.";charset='UTF-8'";
        $this->opt = array(
            PDO::ATTR_ERRMODE               => PDO::ERRMODE_EXCEPTION,
            PDO::ATTR_DEFAULT_FETCH_MODE    => PDO::FETCH_ASSOC
        );
        $this->db = new PDO($this->dsn, DB_USER, DB_PASS, $this->opt);
    }
}

$connection = new MySQLDatabase();

?>

Please help me to solve the issue

Answer the question

In order to leave comments, you need to log in

4 answer(s)
A
Alexey, 2016-10-08
@alsopub

Access denied for user 'root'@'localhost' (using password: YES) - the password for the database does not match.

G
Grigory Esin, 2016-10-08
@xotey83

Going to the database under the "root" user is not secure. Are you sure the password for the "root" user is 111111

F
fomenko_alexandr, 2016-10-08
@fomenko_alexandr

Yes, I check it locally. There the data is root:111111, well, or just under root:and an empty password.
But before writing a class with a connection to pdo, I checked the connection to the database on mysqli, everything works, so the data is 100% correct
Maybe I have something wrong in the class design doing?
I checked on the hosting, with valid data, there was an error:

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY000] [2019] Can't initialize character set 'UTF8' (path: /usr/share/mysql/charsets/)' in /var/www/kitlhin/data /www/kit.lh1.in/test.php:16 Stack trace: #0 /var/www/kitlhin/data/www/kit.lh1.in/test.php(16): PDO->__construct('mysql :host=loca...', 'kit_root', 'Fqwer1A123a', Array) #1 /var/www/kitlhin/data/www/kit.lh1.in/test.php(20): MySQLDatabase->__construct( ) #2 {main} thrown in /var/www/kitlhin/data/www/kit.lh1.in/test.php on line 16

A
Alexander Taratin, 2016-10-08
@Taraflex

Try utf8 instead of UTF-8

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question