Curious13
Curious13

Reputation: 329

PHP PDO DB Connection Unknown character set

I'm at a loss on what I'm doing wrong with my PDO connection to PHPmyadmin mysql. I know this question has been asked numerous times on other SO articles and I looked up this one and I'm still having issues. I don't know what I'm doing wrong.

Here is the SO article similar to my problem I'm trying to solve.

Other SO problem similiar to mine

Here is my configuration settings:

MySQL version: 10.1.38-MariaDB - mariadb.org binary distribution

PHP version: 5.6.40

Server charset: UTF-8 Unicode (utf8)

DB Connection File:

<?php
session_start();
//error_reporting(E_ERROR | E_PARSE | E_CORE_ERROR);
$dbhost_name = "localhost";
$database = "testdb";
$username = "root";
$charset='utf8mb4';          
$password = "";         


try {
$dbo = new PDO('mysql:host='.$dbhost_name.';dbname='.$database.';charset='.$charset.$username, $password);
} catch (PDOException $e) {
print "Error!: " . $e->getMessage() . "<br/>";
die();
}
?>

In my config.inc.php I set the following rules:

[mysql]
default-character-set = utf8mb4

[mysqld]
character-set-client-handshake = FALSE
character-set-server=utf8mb4

[client]
default-character-set=utf8mb4

Upvotes: -1

Views: 1948

Answers (1)

Dave
Dave

Reputation: 5191

The period between the charset variable and the user name variable is resulting in an invalid charset. As shown the charset being seen by the PDO constructor is utf8mb4root. Changing that period to a comma so that it meets the specification of the PDO constructor will result in the connection working as expected.

The corrected code is:

try {
$dbo = new PDO('mysql:host  =' . $dbhost_name .
                  ';dbname  =' . $database    .
                  ';charset =' . $charset     ,  // this was a period, needed to be a comma
                                 $username    ,
                                 $password);
} catch (PDOException $e) {
    print "Error!: " . $e->getMessage() . "<br/>";
    die();
}

Upvotes: 1

Related Questions