> ## Documentation Index
> Fetch the complete documentation index at: https://kb.hosting.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Specifying the MySQL character set in PHP

> Learn to set the character set for MySQL connections using mysqli or PDO in PHP.

This article describes how to specify the character set when you connect to a MySQL database using one of the following methods:

* MySQL Improved (*mysqli*) PHP extension

* PDO (PHP Data Objects)

<Note>
  This article assumes that you already know how to connect to a MySQL database using PHP. For information about how to do this, please see [this article](/docs/connect-to-mysql-using-php).
</Note>

## Determining which character sets are available on a server

To determine which character sets are available on your server for MySQL, log in to your account [using SSH](/docs/using-ssh-secure-shell), and then type the following command:

```bash theme={null}
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.

<Tip>
  To view additional information about a character set, such as its detailed description, open the */usr/share/mysql/charsets/Index.xml* file in a text editor.
</Tip>

## Setting the character set using the MySQL Improved extension

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*:

```php theme={null}
$mysqli = new mysqli("localhost", "dbuser", "password", "database");

$mysqli->set_charset("cp1256");
```

## Setting the character set using PDO (PHP Data Objects)

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:

```php theme={null}
$myPDO = new PDO('mysql:host=localhost;dbname=database;charset=utf8;', 'dbuser', 'password');
```

## More information

* To view the official online documentation for the MySQL Improved extension, please visit [http://www.php.net/manual/en/book.mysqli.php](http://www.php.net/manual/en/book.mysqli.php).

* To view the official online documentation for PDO, please visit [http://www.php.net/manual/en/book.pdo.php](http://www.php.net/manual/en/book.pdo.php).

## Related articles

* [Connecting to MySQL using PHP](/docs/connect-to-mysql-using-php)

* [Converting a MySQL database to UTF-8](/docs/convert-mysql-database-utf-8)

* [Managing MySQL databases, users, and tables from the command line](/docs/managing-mysql-databases-and-users-from-the-command-line)
