php连接数据设置字符编码
$conn = new mysqli("127.0.0.1", "user", "pwd", "db_name");
$sql = "insert into test_db values('','这里是标题','这里是描述信息sss','url','图片地址','日期','11')";
//echo $sql;
$conn->query("set character set 'utf8'");//读库
$conn->query("set names 'utf8'");//写库
实例
<!-- Javascript goes in the document HEAD -->
<script type="text/javascript">
function altRows(id){
if(document.getElementsByTagName){
var table = document.getElementById(id);
var rows = table.getElementsByTagName("tr");
for(i = 0; i < rows.length; i++){
if(i % 2 == 0){
rows[i].className = "evenrowcolor";
}else{
rows[i].className = "oddrowcolor";
}
}
}
}
window.onload=function(){
altRows('alternatecolor');
}
</script>
<!-- CSS goes in the document HEAD or added to your external stylesheet -->
<style type="text/css">
table.altrowstable {
font-family: verdana,arial,sans-serif;
font-size:11px;
color:#333333;
border-width: 1px;
border-color: #a9c6c9;
border-collapse: collapse;
}
table.altrowstable th {
border-width: 1px;
padding: 8px;
border-style: solid;
border-color: #a9c6c9;
}
table.altrowstable td {
border-width: 1px;
padding: 8px;
border-style: solid;
border-color: #a9c6c9;
}
.oddrowcolor{
background-color: #ffffff;
}
.evenrowcolor{
background-color: #dee3e5;
}
</style>
<?php
/**
* Created by PhpStorm.
* User: kangyi
* Date: 16/8/14
* Time: 18:10
*/
date_default_timezone_set('Asia/Shanghai');
function show_article()
{
$conn = new mysqli("127.0.0.1", "user", "pwd", "db_name");
$sql = "insert into test_db values('','这里是标题','这里是描述信息sss','url','图片地址','日期','11')";
//echo $sql;
$conn->query("set character set 'utf8'");//读库
$conn->query("set names 'utf8'");//写库
$result = $conn->query($sql);
$sql = "SELECT title,article_desc,article_img,article_url,category,time_stamps FROM test_db";
$result = $conn->query($sql);
echo '<font face="verdana">';
echo '<table class="altrowstable" id="alternatecolor">';
$num_fields = $result->field_count;
//var_dump($num_fields);
$fields = $result->fetch_fields();
// var_dump($fields);
echo '<tr>';
for ($i = 0; $i < $num_fields; $i++) {
echo '<td>' . $fields[$i]->name . '</td>';
}
echo '</tr>';
while ($row = $result->fetch_assoc()) {
echo "<tr></b>";
for ($j = 0; $j < $num_fields; $j++) {
echo '<td>';
echo $row[$fields[$j]->name];
echo '</td>';
}
echo "</tr></b>";
}
echo "</table></b>";
echo "</font>";
$conn->close();
}
show_article();
网友评论