P
P
Pchelkin2015-08-26 01:05:33
PHP
Pchelkin, 2015-08-26 01:05:33

No database connection?

I want to connect to the database, but I get an error: SQLSTATE[HY000] [1193] Unknown system variable 'NAME'
. What could be the error?
database.php

<?php
class Database extends PDO{
    public function __construct() {
        parent::__construct(
                'mysql:host='.DB_HOST.
                ';dbname='.DB_NAME,
                DB_USER,DB_PASS,array(
                    PDO::MYSQL_ATTR_INIT_COMMAND =>"SET NAME 'utf8'".DB_CHAR)
                );
        }
}

And the config.php file
define('DB_HOST', 'localhost');
define('DB_USER', 'admin');
define('DB_PASS', '123456');
define('DB_NAME', 'test');
define('DB_CHAR', 'utf8');

Answer the question

In order to leave comments, you need to log in

3 answer(s)
V
Vitaliy Orlov, 2015-08-26
@webdjuice

there seems to be an error here too:
it should be
"SET NAMES '".DB_CHAR."'"

I
Ildar Saribzhanov, 2015-08-26
@Bluz

Judging by the text of the error, you are told that we do not understand the NAME variable. I'm not the biggest expert in MySQL and PDO in particular, but I think you should write 'SET NAMES utf8'.

A
AlikDex, 2015-08-26
@AlikDex

PDO::MYSQL_ATTR_INIT_COMMAND =>"SET NAMES ".DB_CHAR)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question