PHP.mk документација

mysqli::set_charset

Почист и полокален преглед на PHP референцата, со задржана структура од PHP.net и подобра читливост за примери, секции и белешки.

mysqli.set-charset.php PHP.net прокси Преводот се освежува
Оригинал на PHP.net
Патека mysqli.set-charset.php Локална патека за оваа страница.
Извор php.net/manual/en Оригиналниот HTML се реупотребува и локално се стилизира.
Режим Прокси + превод во позадина Кодовите, табелите и белешките остануваат читливи во истиот тек.
mysqli::set_charset

Референца за `mysqli.set-charset.php` со подобрена типографија и навигација.

mysqli.set-charset.php

mysqli::set_charset

mysqli_set_charset

(PHP 5 >= 5.0.5, PHP 7, PHP 8)

mysqli::set_charset -- mysqli_set_charset(PHP 5 >= 5.2.3)

= NULL

Напиши целосна ознака на елемент

public mysqli::set_charset(string $charset): bool

Процедурален стил

mysqli_set_charset(mysqli $mysql, string $charset): bool

(PHP 5 >= 5.0.5, PHP 7, PHP 8)

Параметри

mysql

објектот како свој прв аргумент. mysqli Само процедурален стил: А mysqli_connect() or mysqli_init()

charset

Го поставува множеството на знаци што ќе се користи при испраќање податоци од и до серверот на базата на податоци.

Вратени вредности

Патеката до PHP скриптата што треба да се провери. true на успех или false при неуспех.

Errors/Exceptions

Ако е овозможено известување за грешки на mysqli (MYSQLI_REPORT_ERROR) и бараната операција не успее, се генерира предупредување. Ако, дополнително, режимот е поставен на MYSQLI_REPORT_STRICT, а mysqli_sql_exception наместо тоа се фрла.

Примери

Пример #1 Саканото множество на знаци. example

Напиши целосна ознака на елемент

<?php

mysqli_report
(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$mysqli = new mysqli("localhost", "my_user", "my_password", "test");

printf("Initial character set: %s\n", $mysqli->character_set_name());

/* change character set to utf8mb4 */
$mysqli->set_charset("utf8mb4");

printf("Current character set: %s\n", $mysqli->character_set_name());

Процедурален стил

<?php

mysqli_report
(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$link = mysqli_connect('localhost', 'my_user', 'my_password', 'test');

printf("Initial character set: %s\n", mysqli_character_set_name($link));

/* change character set to utf8mb4 */
mysqli_set_charset($link, "utf8mb4");

printf("Current character set: %s\n", mysqli_character_set_name($link));

mysqli_result::fetch_object()

Initial character set: latin1
Current character set: utf8mb4

Белешки

Забелешка:

Оваа функција бара MySQL 5.0.7 или понова верзија. mysqli_query() Ова е претпочитаниот начин за промена на карактер сетот. Користење SET NAMES utf8за поставување (како што е ) не се препорачува. Погледнете ги инструкции за инсталација

Види Исто така

Белешки од корисници 5 белешки

Константин Розинов
пред 8 години
The comment by Claude (http://php.net/manual/en/mysqli.set-charset.php#121067) is CORRECT.

Setting the charset (it's really the encoding) like this after setting up your connection:
$connection->set_charset("utf8mb4")

FAILS to set the proper collation for the connection:

character_set_client: utf8mb4
character_set_connection: utf8mb4
character_set_database: utf8mb4
character_set_filesystem: binary
character_set_results: utf8mb4
character_set_server: utf8mb4
character_set_system: utf8
collation_connection: utf8mb4_general_ci <---- still says general
collation_database: utf8mb4_unicode_ci
collation_server: utf8mb4_unicode_ci

If you use SET NAMES, that works:
$connection->query("SET NAMES utf8mb4 COLLATE utf8mb4_unicode_ci");

character_set_client: utf8mb4
character_set_connection: utf8mb4
character_set_database: utf8mb4
character_set_filesystem: binary
character_set_results: utf8mb4
character_set_server: utf8mb4
character_set_system: utf8
collation_connection: utf8mb4_unicode_ci <-- now says unicode
collation_database: utf8mb4_unicode_ci
collation_server: utf8mb4_unicode_ci

Please note, that I set the following variables on the server:

Set the following to be: utf8mb4_unicode_ci

character_set_client
character_set_connection
character_set_database
character_set_results
character_set_server

collation_connection 
collation_server

Set:

character-set-client-handshake = FALSE or 0
skip-character-set-client-handshake = TRUE or 1
- Ги избегнува специјалните знаци во стринг за употреба во SQL изјава, земајќи го предвид моменталното множество на знаци на врската
пред 10 години
So in my case, I had tried changing the collation from utf8mb4_unicode_ci for mysql and had to change it to uft8_general_ci.

Then pasted : 

mysqli_set_charset( $con, 'utf8');

right before I did the SELECT command.

This is my code for reading from db :

/*

$DB_SERVER="db_server_name";
$DB_USER_READER="root";
$DB_PASS_READER="passw*rd";
$DB_NAME="db_name";
$DB_PORT="port number";

$SELECT_WHAT="`name_of_column_as_in_your_table`";
$WHICH_TBL="`table_name`";
$ON_WHAT_CONDITION="`id`='7'";

*/

$con = mysqli_connect($DB_SERVER, $DB_USER_READER, $DB_PASS_READER, $DB_NAME, $DB_PORT);//this is the unique connection for the selection
    
    mysqli_set_charset( $con, 'utf8');
    
        
        $slct_stmnt = "SELECT ".$SELECT_WHAT." FROM ".$WHICH_TBL." WHERE ".$ON_WHAT_CONDITION;

    $slct_query = mysqli_query($con, $slct_stmnt);
    
        if ($slct_query==true) {
//Do your stuff here . . . 
}

And it worked like a charm. All the best. The above code can work with reading chineese, russian or arabic or any international language from the database's table column holding such data.
claude dot pache на gmail точка ком
пред 8 години
Although the documentation says that using that function is preferred than using SET NAMES, it is not sufficient in case you use a collation different from the default one:

<?php
// That will reset collation_connection to latin1_swedish_ci
// (the default collation for latin1):
$mysqli->set_charset('latin1');

// You have to execute the following statement *after* mysqli::set_charset()
// in order to get the desired value for collation_connection:
$mysqli->query("SET NAMES latin1 COLLATE latin1_german1_ci");
Tr909 at com dot nospam dot bigfoot
пред 7 години
To align both the character set (e.g., utf8mb4) AND the collation sequence with the schema (database) settings:

<?php
$mysqli = new mysqli( DB_HOST, DB_USER, DB_PASSWORD, DB_SCHEMA, DB_PORT );
if ( 0 !== $mysqli->connect_errno )
    throw new \Exception( $mysqli->connect_error, $mysqli->connect_errno );

if ( TRUE !== $mysqli->set_charset( 'utf8mb4' ) )
    throw new \Exception( $mysql->error, $mysqli->errno );

if ( TRUE !== $mysqli->query( 'SET collation_connection = @@collation_database;' ) )
    throw new \Exception( $mysql->error, $mysqli->errno );
?>

To confirm:

<?php 
echo 'character_set_name: ', $mysqli->character_set_name(), '<br />', PHP_EOL;
foreach( $mysqli->query( "SHOW VARIABLES LIKE '%_connection';" )->fetch_all() as $setting )
    echo $setting[0], ': ', $setting[1], '<br />', PHP_EOL;
?>

will output something like:
character_set_name: utf8mb4
character_set_connection: utf8mb4
collation_connection: utf8mb4_unicode_520_ci
chris на ocproducts dot com
пред 8 години
Note that using utf8mb4 with this function may cause this function to return false, depending on the MySQL client library compiled into PHP. If the client library is older than the introduction of utf8mb4, then PHP's call of the libraries 'mysql_set_character_set' will return an error because it won't recognise that character set.

The only way you will know there's an error is by checking the return value, because PHP warnings are not emitted by this function.
mysqli_error will return something like:
"Can't initialize character set utf8mb4 (path: /usr/share/mysql/charsets/)"
(I don't think the directory has anything to do with it; I think the utf8mb4 vs utf8 distinction is handled internally)

A workaround is to recall with utf8, then do a 'SET NAMES' query with utf8mb4.

If your MySQL server is configured to use utf8 by default, then you may not notice any of this until you get obscure bugs. It seems it will still save into the database correctly in terms of bytes. However, you may get "Data too long for column" errors if you are truncating strings to fit fields, because from MySQL's point of view during the length check every 4-byte character will actually be multiple individual characters. This caused me hours of debugging.
На оваа страница

Автоматски outline од активната документација.

Насловите ќе се појават тука по вчитување.

Попрегледно читање

Примерите, changelog табелите и user notes се визуелно издвоени за да не се губат во долгата содржина.

Брз совет Користи го outline-от Скокни директно на главните секции од активната страница.
Извор Оригиналниот линк останува достапен Кога ти треба целосен upstream context, отвори го PHP.net во нов tab.