add utf8mb4 to mysql code example

Example 1: set utf8mb4 mysql tables

--For each database:

ALTER DATABASE
    database_name
    CHARACTER SET = utf8mb4
    COLLATE = utf8mb4_unicode_ci;
    
--For each table:

ALTER TABLE
    table_name
    CONVERT TO CHARACTER SET utf8mb4
    COLLATE utf8mb4_unicode_ci;
    
--For each column:

ALTER TABLE
    table_name
    CHANGE column_name column_name
    VARCHAR(191)
    CHARACTER SET utf8mb4
    COLLATE utf8mb4_unicode_ci;

Example 2: mysqli set utf8 encoding

<?php
$mysqli = new mysqli("localhost","my_user","my_password","my_db");

if ($mysqli -> connect_errno) {
  echo "Failed to connect to MySQL: " . $mysqli -> connect_error;
  exit();
}

echo "Initial character set is: " . $mysqli -> character_set_name();

// Change character set to utf8
$mysqli -> set_charset("utf8");

echo "Current character set is: " . $mysqli -> character_set_name();

$mysqli -> close();
?>

Tags:

Sql Example