<?php
$dbName    = "test";
$tableName = "testUtf8mb4";
$host      = "127.0.0.1";
$username  = "maxuser";
$password  = "maxpwd";
$port      = "4006";


$connection = new PDO("mysql:host=$host;port=$port;dbname=$dbName;charset=utf8mb4", $username, $password, []);
$connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

$createQuery = "CREATE OR REPLACE TABLE `$dbName`.`$tableName` (
	`a` serial,
	`ID` binary(16) NOT NULL,
	`content` text NOT NULL,
	PRIMARY KEY (`ID`)
) ENGINE=`InnoDB` DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci ROW_FORMAT=COMPACT COMMENT='' CHECKSUM=0 DELAY_KEY_WRITE=0;";

$insertQuery = "INSERT INTO `$dbName`.`$tableName` (`ID`, `content`) VALUES (:id, :content)";

$selectQuery = "SELECT *, @@last_insert_id FROM `$dbName`.`$tableName` ORDER BY a";

$connection->exec($createQuery);
$connection->exec("DELETE FROM `$dbName`.`$tableName`");

$messages = [
    '🙈😂😱🍰',
    '💩💩💩💩',
    '🙈😂😱🍰💩💩💩💩',
    '💩💩💩💩🙈😂😱🍰'
];
echo "Write\r\n";
foreach ($messages AS $message) {
    $hex   = bin2hex($message);
    $id    = random_bytes(16);
    $idHex = bin2hex($id);
    echo "($idHex) $message - $hex\r\n";

    $sth = $connection->prepare($insertQuery);
    $sth->execute([':id' => $id, ':content' => $message]);
}
$sth->closeCursor();

echo "Read\r\n";
$sth = $connection->prepare($selectQuery);
$sth->execute();
$results = $sth->fetchAll(PDO::FETCH_ASSOC);
$sth->closeCursor();

foreach ($results AS $result) {
    $hex     = bin2hex($result['content']);
    $message = $result['content'];
    $idHex   = bin2hex($result['ID']);
    echo "($idHex) $message - $hex\r\n";
}