作者c182796378 (似曾相似)
看板PHP
标题[请益] 读取资料库显示中文
时间Sat Jan 6 02:52:49 2018
如题,已爬过许多文章,尝试了很多方式
读取资料库资料,显示出来的中文都变成了问号QQ
以下为程式码:
<?php
//fetch.php
header('Content-Type: text/html; charset=utf-8');
$connect = mysqli_connect("localhost", "root", "12345678", "contact");
mysql_query("SET NAMES 'utf-8'");
mysql_query("SET CHARACTER_SET_CLIENT='utf-8'");
mysql_query("SET CHARACTER_SET_RESULTS='utf-8'");
$output = '';
if(isset($_POST["query"]))
{
$search = mysqli_real_escape_string($connect, $_POST["query"]);
$query = "
SELECT * FROM contact
WHERE name LIKE '%".$search."%'
OR gender LIKE '%".$search."%'
OR phone LIKE '%".$search."%'
OR mobile LIKE '%".$search."%'
";
}
else
{
$query = "
SELECT * FROM contact ORDER BY name
";
}
$result = mysqli_query($connect, $query);
if(mysqli_num_rows($result) > 0)
{
$output .= '
<div class="table-responsive">
<table class="table table bordered">
<tr>
<table width="805" height="75" border="0" id="test" border: solid #0
00
<tr bgcolor="#A9D6EB">
<th>C_name</th>
<th>C_id</th>
<th>C_phone</th>
<th>C_staus</th>
</tr>
';
while($row = mysqli_fetch_array($result))
{
$output .= '
<tr>
<td>'.$row["name"].'</td>
<td>'.$row["gender"].'</td>
<td>'.$row["phone"].'</td>
<td>'.$row["mobile"].'</td>
</tr>
' ;
}
echo $output;
}
else
{
echo 'Data Not Found';
}
?>
以下为网页显示画面跟PHPmyadmin:
https://imgur.com/a/jqmBo
https://imgur.com/a/u4ZkE
先谢谢各位大大了~
--
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 114.33.79.26
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/PHP/M.1515178371.A.77D.html
1F:→ a0960330: 你的HTML没有 charset=utf-8 ?01/06 10:03
有加了耶,但还是一样显示问号
※ 编辑: c182796378 (27.247.44.157), 01/06/2018 12:16:47
2F:→ tkdmaf: 这麽多内容的code是多余的,在问问题时应该要缩小范围01/06 13:23
3F:→ tkdmaf: 你的问题既然是编码,只要最低限度的测试输出结果01/06 13:23
4F:→ tkdmaf: 然後你并没有告知你资料库的编码结构01/06 13:24
5F:→ tkdmaf: 资料库方面还有资料库编码,资料表编码………01/06 13:26
资料库编码结构是指utf8_unicode_ci吗?抱歉QQ因为刚开始学php,很多东西都还搞不清
楚...
※ 编辑: c182796378 (27.247.44.157), 01/06/2018 13:31:10
※ 编辑: c182796378 (114.33.79.26), 01/06/2018 13:46:40
6F:→ wbc319: 直接连结资料库 echo 中文那里...出来的也是问号吗?01/06 13:51
echo可以显示中文!但读取资料就不行
※ 编辑: c182796378 (27.247.44.157), 01/06/2018 13:59:48
※ 编辑: c182796378 (27.247.44.157), 01/06/2018 14:00:26
7F:→ wbc319: ("SET NAMES 'utf-8'"); 是少一个=吗? 01/06 14:48
8F:→ JohnRoyer: Study-Area 之前有篇文章有详细的设定,可以参考 01/06 15:58
10F:→ fezexp9987: 话说 mysqli和mysql能混着用@@? 01/06 18:05
11F:→ fezexp9987: 不过印象中没有 - 不知道多了行不行 SET NAMES UTF8 01/06 18:16
12F:→ a0960330: mysqli跟mysql应该不能混着用吧......会不会是你用 01/06 21:31
13F:→ a0960330: mysqli连接资料库,可是你却用mysql设定utf8 01/06 21:31
14F:→ gpmm: 你资料表的语系是? 01/06 23:11
15F:推 hiigara: phpmyadmin 里面下个 show create table customer_data 01/06 23:16
16F:→ hiigara: 然後把结果印出来给大家看看 01/06 23:16
17F:→ hiigara: 等等,mysql 的编码定义应该是 utf8 而不是 utf-8? 01/06 23:18
18F:→ hiigara: utf8 或 utf8mb4(大部分的表情符号要後者才会动) 01/06 23:19
20F:推 crazwade: 资料库的栏位好像也要设编码 01/08 16:18
21F:→ MOONRAKER: 整个资料库编码设对就不用逐个设啦 01/08 16:33