How to define charset UTF-8

I’ve seen many ways of defining and might have created a mix of defining things on php. Can you help me edit thingws together:

<?php 
// for display
ob_start();
//header("Content-Type: text/html; charset=utf-8");
//header("Content-Type: text/html; charset=utf-8_general_ci");
//error_reporting(E_ALL);
ini_set('display_errors', true);
set_time_limit(480);
//define("DSN", "mysql:host=localhost;dbname=biblewheel;charset=utf-8");
define("DSN", "mysql:host=localhost;dbname=biblewheel;charset=utf-8_general_ci");

define("USERNAME", "root");

define("PASSWORD", "");

/*$pdo = new PDO('mysql:host=hostname;dbname=database', 'username', 'password',
    array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8")
);
*/

$options = array(PDO::ATTR_PERSISTENT =>true);

try{
    $conn = new PDO(DSN, USERNAME, PASSWORD, $options);
    //array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf-8_general_ci COLLATE utf8"); utf8mb4_bin
    //$conn = new PDO("mysql:host=localhost; dbname=biblewheel;charset=UTF8","root","",array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"))or die("no connection"); $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    
    //echo "connection successful<br /><br />\n";
}catch (PDOException $ex){
    echo "A database error occurred ".$ex->getMessage();    
}

Try this site because it is quite comprehensive and covers about everything there is to know about DataBases:

https://phpdelusions.net/pdo#dsn

I use this script for connecting to the DataBase and it also hides the login details:

<?php 
  declare(strict_types=1);

  ini_set('display_errors', 'true');
  error_reporting(-1);

# SET DBASE PARAMETERS ONLINE OR LOCALHOST
  $fileAboveRoot = '/var/www/html/above_root/_database_params.php';
  if(file_exists( $fileAboveRoot) ): 
    # PREVENT HACKING
      include $fileAboveRoot;
  else: // LOCALHOST:
      define('DB_HOST', 'localhost'); 
      define('DB_USER', 'fi03&3^6!1+4|6@25035');
      define('DB_PWRD', 'Na#0$3%3^6&1*4(625035!@#');
      define('DB_NAME', 'file_integrity');
  endif;  

  # CONNECT
    try
    {
      $PDO = new PDO
      (
        "mysql:host=" .DB_HOST . "; 
         dbname="     .DB_NAME,  DB_USER,  DB_PWRD
      );
      define('DSN', 'localhost');
      echo '<br>Connected OK';
      # STATUS
        $attributes = array
        (
          "AUTOCOMMIT", 
          "ERRMODE", 
          "CASE", 
          "CLIENT_VERSION", 
          "CONNECTION_STATUS",
          "ORACLE_NULLS", 
          "PERSISTENT", 
          "SERVER_INFO", 
          "SERVER_VERSION",
          "TIMEOUT",
          "PREFETCH", 
        );
        echo '<dl>';
          foreach ($attributes as $val) :
              try {
                echo "<dt>PDO::ATTR_$val: </dt>";
                echo '<dd>' .$PDO->getAttribute(constant("PDO::ATTR_$val")) .'</dd>';
              }catch (Exception $e) {
                echo($e. ' ==> '. __line__);
              } 
              echo '<dt> &nbsp; <dd>';
          endforeach; // }
        echo '</dl>';

    }catch (Exception $e) {
      echo($e. ' ==> '. __line__);
    } 
    $PDO->exec("set names utf8");
die;

EDIT
<dl> typo changed to </dl> after the endforeach
* as per following replies

how does it reinterpret the username and password then? How does that work?

Did you try running the script:

  1. without changing the Defines
  2. Setting the define constants to your login details
  3. Creating a file above the root with your login details

If the database logged in ok the database parameters will show.

Is this xml? I don’t think it would work with my html because I see <dl>.

<dl> is an html tag for definition list.

2 Likes

Whoops. Yes it is HTML and the closing <dl> tag is incorrect and should have been </dl>.

The idea is to render the results and make it easier to view rather than a single text string.

The script will render ok, even with incorrect closing tag but would have not been HTML syntactically correct.

Try it using PHP to view the database settings.

Edit:
The HTML definition list is useful for displaying results.

https://www.w3schools.com/tags/tag_dl.asp

oh it’s like ol and ul then.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.