This article describes how to specify the character set when you connect to a MySQL database using one of the following methods:
To determine which character sets are available on your server for MySQL, log in to your account using SSH, and then type the following command:
grep "charset name" /usr/share/mysql/charsets/Index.xml | cut -f2 -d'"'
This command displays the list of available values that you can use in the methods described below.
If you are using the MySQL Improved (mysqli) extension, use the set_charset method to specify the character set. For example, the following sample code demonstrates how to specify the Windows Arabic character set using mysqli:
$mysqli = new mysqli("localhost", "dbuser", "password", "database"); $mysqli->set_charset("cp1256");
To specify the character set using PDO, all you have to do is include the charset setting in the connection string. For example, the following sample code demonstrates how to specify the UTF-8 Unicode character set using PDO:
$myPDO = new PDO('mysql:host=localhost;dbname=database;charset=utf8;', 'dbuser', 'password');
Subscribe to receive weekly cutting edge tips, strategies, and news you need to grow your web business.
No charge. Unsubscribe anytime.
Did you find this article helpful? Then you'll love our support. Experience the A2 Hosting difference today and get a pre-secured, pre-optimized website. Check out our web hosting plans today.
We use cookies to personalize the website for you and to analyze the use of our website. You consent to this by clicking on "I consent" or by continuing your use of this website. Further information about cookies can be found in our Privacy Policy.