How to set collation for MySQL Connection?
// Here is default/Expected output
mysqli_get_charset($con);
object(stdClass)[2]
public 'charset' => string 'utf8mb4' (length=7)
public 'collation' => string 'utf8mb4_unicode_ci' (length=18)
public 'dir' => string '' (length=0)
public 'min_length' => int 1
public 'max_length' => int 4
public 'number' => int 224
public 'state' => int 1
public 'comment' => string '' (length=0)
mysqli_character_set_name($con);
\tests\db.php:22:string 'utf8mb4' (length=7)
After setting charset using :
mysqli_set_charset($con, 'utf8mb4');
// Output :
mysqli_get_charset($con);
object(stdClass)[2]
public 'charset' => string 'utf8mb4' (length=7)
public 'collation' => string 'utf8mb4_general_ci' (length=18)
public 'dir' => string '' (length=0)
public 'min_length' => int 1
public 'max_length' => int 4
public 'number' => int 45
public 'state' => int 1
public 'comment' => string 'UTF-8 Unicode' (length=13)
mysqli_character_set_name($con);
\tests\db.php:22:string 'utf8mb4' (length=7)
Tried using these :
mysqli_set_charset($con, 'utf8mb4');
mysqli_query($con, "SET NAMES 'utf8mb4';");
mysqli_query($con, "SET CHARACTER SET 'utf8mb4';");
mysqli_query($con, "SET COLLATION_CONNECTION = 'utf8mb4_unicode_ci';");
After using mysqli_set_charset($con, 'utf8mb4');
the collation resets to utf8mb4_general_ci.
Any way to prevent this.
How can I solve this ?