search
HomeBackend DevelopmentPHP TutorialSummary of problems with PHP and Mysql transferring UTF8 encoding, mysql transferring UTF8 encoding_PHP tutorial

A summary of problems with PHP and Mysql transferring UTF8 encoding, mysql transferring UTF8 encoding

If a website needs to be internationalized, it needs to convert the encoding from GB2312 to UTF-8, there are many of them You need to pay attention to the problem. If the conversion is not complete, there will be many encoding problems!
Converting PHP page to UTF-8 encoding problem
1. Add a line at the beginning of the code: header("Content-Type: text/html;charset=utf-8");
2. PHP file encoding problem. Click the menu of the editor: "File" -> "Save As", you can see the encoding of the current file. Make sure the file encoding is: UTF-8. If it is ANSI, you need to change the encoding to: UTF-8.
3. PHP file header BOM problem: PHP files must not have BOM tags, otherwise, the session will not be usable, and there will be similar prompts: Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent This is because when executing session_start(), the entire page cannot have output, but when the BOM tag exists in the previous PHP page, PHP treats the BOM tag as output, so an error occurs! Therefore, the BOM tag must be deleted from the PHP page
How to delete this BOM tag:
1. You can open the file with Dreamweaver and resave it to remove the BOM tag!
2. You can open the file with EditPlus,
and in the menu "Preferences"->"File"->"UTF-8 Identity", set it to: "Always delete signature", and then save the file. Remove the BOM tag!
3. UTF-8 encoding problem when PHP saves files as attachments: When PHP saves files as attachments, the file name must be GB2312 encoded, otherwise, if there is Chinese in the file name, it will be displayed Garbled characters: If your PHP itself is a file in UTF-8 encoding format, you need to convert the file name variable from UTF-8 to GB2312: iconv("UTF-8", "GB2312", "$filename");
4. When truncating and displaying the article title, garbled characters or "?" question marks appear:
Generally, when the title of an article is very long, part of the title will be displayed and the title of the article will be truncated. Since a Chinese character in UTF-8 encoding format will occupy 3 characters in width, when the title is intercepted, sometimes only the If the 1 character or 2 character width of a Chinese character is not completely intercepted, garbled characters or "?" question marks will appear. Use the following function to intercept the title and there will be no problem:

function get_brief_str($str, $max_length) { 
  echo strlen($str) . ""; 
  if (strlen($str) > $max_length) { 
    $check_num = 0; 
    for ($i = 0; $i < $max_length; $i++) { 
      if (ord($str[$i]) > 128) 
        $check_num++; 
    } 
 
    if ($check_num % 3 == 0) 
      $str = substr($str, 0, $max_length) . "..."; 
    else 
      if ($check_num % 3 == 1) 
        $str = substr($str, 0, $max_length +2) . "..."; 
      else 
        if ($check_num % 3 == 2) 
          $str = substr($str, 0, $max_length +1) . "..."; 
  } 
 
  return $str; 
}

The problem of using UTF-8 encoding in MYSQL database
1. Use phpmyadmin to create the database and data table. When creating the database, please set "Organization" to: "utf8_general_ci" or execute the statement:

CREATE DATABASE `dbname` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;

When creating a data table: If the field stores Chinese, you need to set "Organization" to: "utf8_general_ci". If the field stores English or numbers, the default is fine.
Corresponding SQL statement, for example:

CREATE TABLE `test` ( 
`id` INT NOT NULL , 
`name` VARCHAR( 10 ) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL , 
PRIMARY KEY ( `id` ) 
) ENGINE = MYISAM ;

2. Use PHP to read and write databases
After connecting to the database:

$connection = mysql_connect($host_name, $host_user, $host_pass);

Add two lines:

mysql_query("set character set 'utf8'");//读库 
mysql_query("set names 'utf8'");//写库

I hope this article will be helpful to everyone’s PHP MySQL programming.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1060097.htmlTechArticleSummary of problems with PHP and Mysql transfer UTF8 encoding, mysql transfer UTF8 encoding If a website needs to be internationalized, it needs to encode There are many issues that need to be paid attention to when converting from GB2312 to UTF-8, such as...
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
php怎么把负数转为正整数php怎么把负数转为正整数Apr 19, 2022 pm 08:59 PM

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

php怎么实现几秒后执行一个函数php怎么实现几秒后执行一个函数Apr 24, 2022 pm 01:12 PM

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php怎么除以100保留两位小数php怎么除以100保留两位小数Apr 22, 2022 pm 06:23 PM

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

php怎么根据年月日判断是一年的第几天php怎么根据年月日判断是一年的第几天Apr 22, 2022 pm 05:02 PM

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

php怎么判断有没有小数点php怎么判断有没有小数点Apr 20, 2022 pm 08:12 PM

php判断有没有小数点的方法:1、使用“strpos(数字字符串,'.')”语法,如果返回小数点在字符串中第一次出现的位置,则有小数点;2、使用“strrpos(数字字符串,'.')”语句,如果返回小数点在字符串中最后一次出现的位置,则有。

php字符串有没有下标php字符串有没有下标Apr 24, 2022 am 11:49 AM

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

php怎么替换nbsp空格符php怎么替换nbsp空格符Apr 24, 2022 pm 02:55 PM

方法:1、用“str_replace("&nbsp;","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\&nbsp\;||\xc2\xa0)/","其他字符",$str)”语句。

php怎么读取字符串后几个字符php怎么读取字符串后几个字符Apr 22, 2022 pm 08:31 PM

在php中,可以使用substr()函数来读取字符串后几个字符,只需要将该函数的第二个参数设置为负值,第三个参数省略即可;语法为“substr(字符串,-n)”,表示读取从字符串结尾处向前数第n个字符开始,直到字符串结尾的全部字符。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),