我在mysql数据库中设置了utf8
现在显示
+--------------------------+----------------------------+
| Variable_name | Value |
+--------------------------+----------------------------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | utf8 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | utf8 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
+----------------------+-----------------+
| Variable_name | Value |
+----------------------+-----------------+
| collation_connection | utf8_general_ci |
| collation_database | utf8_unicode_ci |
| collation_server | utf8_unicode_ci |
+----------------------+-----------------+
我在PHP代码中有以下内容
mysqli_query("SET NAMES 'utf8'");
和
$con = mysqli_connect('host','user','pass','database');
mysqli_query($con,"SET NAMES 'utf8'");
由于我在数据库中设置了utf8,我打算删除
mysqli_query("SET NAMES 'utf8'");
和
mysqli_query($database,"SET NAMES 'utf8'");
来自PHP代码
我被告知我可以删除上面的代码,但我需要添加
mysqli_set_charset($con,"utf8");
在PHP代码中。
我可以设置
,而不是在代码中添加mysqli_set_charset($con,"utf8")
default_charset = "UTF-8"
在php.ini
?
my.cnf是
[client]
default-character-set=utf8
[mysqld]
default-character-set=utf8
default-collation=utf8_general_ci
character-set-server=utf8
collation-server=utf8_unicode_ci
init-connect='SET NAMES utf8'
答案 0 :(得分:2)
我可以设置,而不是在代码中添加
import java.io.*; import org.apache.hadoop.fs.FSDataInputStream; import org.apache.hadoop.fs.PositionedReadable; import org.apache.hadoop.fs.Seekable; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; public class SeekableTest { public static void main(String[] args) throws IOException { Resource resource = new ClassPathResource("somefile"); InputStream in = resource.getInputStream(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); byte buf[] = new byte[1024]; int read; while ((read = in.read(buf)) > 0) baos.write(buf, 0, read); byte data[] = baos.toByteArray(); SeekableByteArrayInputStream bais = new SeekableByteArrayInputStream(data); FSDataInputStream in2 = new FSDataInputStream(bais); } static class SeekableByteArrayInputStream extends ByteArrayInputStream implements Seekable, PositionedReadable { public SeekableByteArrayInputStream(byte[] buf) { super(buf); } @Override public long getPos() throws IOException{ return pos; } @Override public void seek(long pos) throws IOException { if (mark != 0) throw new IllegalStateException(); reset(); long skipped = skip(pos); if (skipped != pos) throw new IOException(); } @Override public boolean seekToNewSource(long targetPos) throws IOException { return false; } @Override public int read(long position, byte[] buffer, int offset, int length) throws IOException { if (position >= buf.length) throw new IllegalArgumentException(); if (position + length > buf.length) throw new IllegalArgumentException(); if (length > buffer.length) throw new IllegalArgumentException(); System.arraycopy(buf, (int) position, buffer, offset, length); return length; } @Override public void readFully(long position, byte[] buffer) throws IOException { read(position, buffer, 0, buffer.length); } @Override public void readFully(long position, byte[] buffer, int offset, int length) throws IOException { read(position, buffer, offset, length); } } }
php.ini中的mysqli_set_charset($con,"utf8")
?
不,这不会影响数据库连接字符集。
即使MySQL extension settings在这方面也没有提供任何内容 - those for MySQLi也没有。
毕竟 是一个连接设置。
但是如果你把代码建立数据库连接到一个你需要的文件中,那就足够了。
答案 1 :(得分:0)
问题在于引用MySQL,因此在php.ini中设置配置在这种情况下不会有太大帮助。我建议更新mysql中的my.cnf文件
有关my.cnf设置的更多信息,请访问here
[client]
default-character-set=utf8
[mysqld]
character-set-server=utf8
default-character-set=utf8
default-collation=utf8_unicode_ci
character-set-client = utf8
我从这个thread获取了上述部分,它讨论了同样的问题,但是对于PHP和MySQL都是如此。 希望这有帮助!