Home  >  Article  >  Backend Development  >  How to solve the php sqlite garbled problem

How to solve the php sqlite garbled problem

藏色散人
藏色散人Original
2020-09-29 10:41:352816browse

php SQLite garbled solution: first open the "db.db" table; then save the results to a new array through the query; then convert the column values; finally reprocess the queried data and force all conversions Just change it to UTF8.

How to solve the php sqlite garbled problem

Recommended: "PHP Video Tutorial"

Sqlite3 Chinese garbled problems and solutions in Linux systems

There is no problem when testing a new project locally (Win8), but when it is transmitted to the server (Linux), the Chinese data queried from the Sqlite3 database is garbled (it is normal in the database)

Set both php files and html files to unified utf8, but the same thing is still the same. I also found various solutions on the Internet to no avail.

Solution:

Change the queried The data is reprocessed and all forced to convert to UTF8

//打开db.db表
$db = new SQLite3('db.db');
if(!$db){
   echo $db->lastErrorMsg();
   exit;
} else {
}
$keys = array();
$students = array();
//查询
$ret = $db->query("SELECT * from student limit 50");
//将结果保存到新数组
while($row = $ret->fetchArray(SQLITE3_ASSOC) ){
   $keys = array_keys($row);
   //转换列值,防止出现乱码
   foreach($keys as $key){
      $row[$key] = mb_convert_encoding($row[$key],"gb2312","utf-8");
   }
   //添加到新数组
   array_push($students, $row);
}
$db->close();
print_r($students);

The above is the detailed content of How to solve the php sqlite garbled problem. For more information, please follow other related articles on the PHP Chinese website!

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