PHP 101
  • Home
  • Deploy
  • How-to
  • Tutorial
No Result
View All Result
PHP 101
  • Home
  • Deploy
  • How-to
  • Tutorial
No Result
View All Result
PHP 101
No Result
View All Result
Home How-to

How to set MySQL charset (or MariaDB) by PHP

March 26, 2022
Reading Time: 2 mins read
PHP101.net - How to set MySQL charset by PHP

PHP101.net - How to set MySQL charset by PHP

Share on FacebookShare on Twitter

Quick Solution

To quickly set MySQL charset (for example, to utf8mb4) using PHP for the current database connection session, you must first have access to the connection object (mysqli object), assuming it’s $mysqli:

We can then use either procedural mysqli_set_charset function to set MySQL charset:

mysqli_set_charset( $mysqli, 'utf8mb4' );

Or use the object oriented way:

$mysqli->set_charset( 'utf8mb4' );

That will change the charset to utf8mb4.

The guide can be considered as completed. The below sections will give more examples on the methods.

Set MySQL charset using PHP

Prerequisites to setting MySQL charset is having access to the MySQL connection object.

We will have an example by creating the database connection by initializing mysqli object:

$mysqli = new mysqli( 'localhost', 'user', 'password', 'database' );

Then, getting the initial charset set by the system configuration using character_set_name() method:

$charset = $mysqli->character_set_name();
print_r( $charset );

If the charset is not as expected, now it’s the time to change them. Having the $mysqli connection object, it is now easy to set MySQL charset using the two introduced ways.

1. Set MySQL charset by  mysqli_set_charset function

mysqli_set_charset( $mysqli, 'utf8mb4' );

2. Object oriented style:

$mysqli->set_charset( 'utf8mb4' );

We can verify the changed charset using character_set_name() again.

Final Thoughts

It is very easy to set MySQL charset using PHP. To show all of the supported charset installed with your MySQL/MariaDB, you can use MySQL’s  SHOW_CHARACTER_SET command.

For more information:

External
Character Sets and Collations in MySQL

You might also like

  • How to download file from URL using PHP
  • 3 ways to get PHP memory limit value

References

  • PHP.net Official Documentation – mysqli_set_charset / mysqli::set_charset
  • PHP.net Official Documentation – mysqli_character_set_name / mysqli::character_set_name
  • MySQL Documentation – Character Sets and Collations in MySQL
Previous Post

How to convert from array to string in PHP

Next Post

How to convert from string to array in PHP

Related Posts

PHP101.net - How to set timezone with PHP

How to set timezone with PHP easily (multiple methods)

March 9, 2023
PHP101.net - How to redirect to another page using PHP

How to redirect to another page using PHP

March 8, 2023

Table of Contents

    • Quick Solution
    • Set MySQL charset using PHP
    • Final Thoughts
    • You might also like
    • References
Next Post
PHP101.net - How to convert from string to array in PHP

How to convert from string to array in PHP

We deliver free and helpful resources for learning
and using PHP programming language.

About
Contact Us
Privacy Policy
Main Sections

Home
Deploy
How-to
Tutorial
You might be interested in...

How to change Laragon PHP version on Windows

How to detect visitors by country using PHP & GeoIP

How to install ImageMagick on Windows

Deploy a webserver with XAMPP on Windows for PHP development

How to count array elements in PHP

© 2022 PHP101.Net - Helpful PHP Tutorials, How-to, Tips and Tricks for everyone.

No Result
View All Result
  • Home
  • Deploy
  • How-to
  • Tutorial

© 2022 PHP101.Net - Helpful PHP Tutorials, How-to, Tips and Tricks for everyone. | Privacy Policy

This website uses cookies. By continuing to use this website you are giving consent to cookies being used. Visit our Privacy and Cookie Policy.