-1

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
halfer
  • 19,824
  • 17
  • 99
  • 186
Curious13
  • 329
  • 2
  • 23
  • You have a typo in your PDO statement, change the `.` (period) between `$charset` and `$username` to a comma `,`. That part of the statement should read `';charset='.$charset , $username, $password` – Dave Mar 06 '19 at 19:24
  • @halfer I know what it is doing there and I know that it is the source of the problem. The user and password are separated from the DSN, and each other, in the [PDO constructor](http://php.net/manual/en/pdo.construct.php) with a comma. As the statement stands having them concatenated results in `;charset=utf8mb4root` which results in the error message that is being asked about. – Dave Mar 07 '19 at 09:52
  • @halfer May I humbly suggest that you try my suggestion sir. Changing that period to a comma will in fact work just fine. And just to make sure I hadn't completely lost it I just tried it again to verify that it is the correct solution. – Dave Mar 07 '19 at 13:25
  • @Dave: thanks for posting an answer. My apologies - it looked to me like you wanted the username and password to appear in the same string, and that you'd not concatenated them correctly. Since they are separate parameters, that makes sense to me now. – halfer Mar 07 '19 at 17:24

1 Answers1

1

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();
}
Dave
  • 5,108
  • 16
  • 30
  • 40