error connecting to MYSQL

14,157

Solution 1

Once try with your credentials directly instead of using them as variable.

also try to debug using below:

catch(PDOException $e) {
    error_log($e->getMessage());
    die("A database error was encountered -> " . $e->getMessage() );
}

Let me know if it works.

Solution 2

you simple change UTF-8 to utf8..

try {
      $db = new PDO(
              "mysql:host=$db_host;dbname=$db_name;charset=utf8",
              $db_user,
              $db_pass
            );
Share:
14,157
Steven
Author by

Steven

Updated on July 05, 2022

Comments

  • Steven
    Steven almost 2 years

    Here is the config.php file

    <?php
    
    error_reporting(E_ALL ^ E_NOTICE);
    
    /*=========== Database Configuraiton ==========*/
    
    $db_host = "localhost";
    $db_user = "test";
    $db_pass = "test";
    $db_name = "dbtest";
    
    
    /*=========== Website Configuration ==========*/
    
    $defaultTitle = 'testing';
    $defaultFooter = date('Y').' &copy; testing';
    
    ?>
    

    Here is the reference to config.php

    <?php
    
    
    require_once "includes/config.php";
    require_once "includes/connect.php";
    require_once "includes/helpers.php";
    
    
    
    header('Cache-Control: max-age=3600, public');
    header('Pragma: cache');
    header("Last-Modified: ".gmdate("D, d M Y H:i:s",time())." GMT");
    header("Expires: ".gmdate("D, d M Y H:i:s",time()+3600)." GMT");
    
    ?>
    

    Connect.php is below

    <?php
    
    /*
            The login details are taken from config.php.
    */
    
    try {
        $db = new PDO(
            "mysql:host=$db_host;dbname=$db_name;charset=UTF-8",
            $db_user,
            $db_pass
        );
    
        $db->query("SET NAMES 'utf8'");
        $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    }
    catch(PDOException $e) {
        error_log($e->getMessage());
        die("A database error was encountered");
    }
    
    
    ?>
    

    Does anyone see the problem with this code? I am getting the error message from connect.php "a database error was encountered" I need another set of eyes because all my info looks correct and I can't see the error in the code. Thanks.

  • Steven
    Steven almost 12 years
    I changed all that info so I could post it here, but I had the correct info in there. No it hasn't worked before
  • Steven
    Steven almost 12 years
    SQLSTATE[HY000] [2019] Can't initialize character set UTF-8 (path: /usr/share/mysql/charsets/)
  • Govind Totla
    Govind Totla almost 12 years
    well then you should debug it like : $db = new PDO( "mysql:host=$db_host;dbname=$db_name;", $db_user, $db_pass ); remove charset=UTF-8 at the time of connecting.
  • Steven
    Steven almost 12 years
    is it just a matter or removing the -8 and making it utf8
  • Mike S.
    Mike S. almost 12 years
    also perhaps get rid of tick marks on utf8 so just: $pdo->query("SET NAMES utf8"); I guess depends on what you need to return (query vs. exec) but likely exec is best and just number of rows affected in return.
  • Steven
    Steven almost 12 years
    haha, the UTF-8 instead of utf8 was causing the problem. I knew it was something tiny that I missed. Thanks for all the help Govind.
  • Govind Totla
    Govind Totla almost 12 years
    use this to set char set. $db->exec("SET CHARACTER SET utf8");
  • Mike S.
    Mike S. almost 12 years
    Note others have had issues with this: bugs.php.net/bug.php?id=47224 but you are not using the attribute so should work.