"设置名称' utf8'"这是有$变量

时间:2017-08-14 08:03:26

标签: php html

<?php

$servername = "localhost";
$username = "root";
$password = "";
$dbname = "membership";

$conn = mysqli_connect($servername, $username, $password, $dbname) or die("Connection failed: " . mysqli_connect_error());
/* check connection */
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
} 

$limit = 10;  
if (isset($_GET["page"])) { 
    $page  = $_GET["page"]; 
} else { 
    $page=1; 
}  
$start_from = ($page-1) * $limit;  

$sql = "SELECT * FROM registration ORDER BY id ASC LIMIT $start_from, $limit";  
$rs_result = mysqli_query($conn, $sql);  

?> 
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
            <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.0/css/bootstrap.min.css">
            <script type="text/javascript" charset="utf8" src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-2.0.3.js"></script>
            <script type="text/javascript" src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
            <link rel="stylesheet" href="dist/simplePagination.css" />
            <script src="dist/jquery.simplePagination.js"></script>
            <title></title>
    </head>
    <body>
        <div class="container" style="padding-top:20px;">
            <table class="table table-bordered">  
                <thead>  
                    <tr>  
                        <th>Name</th>  
                        <th>Salary</th>
                        <th>Age</th>  
                    </tr>  
                </thead>  
                <tbody>  
<?php  

while ($row = mysqli_fetch_assoc($rs_result)) {

?>  
                    <tr>  
                        <td><?php echo $row["id"]; ?></td>  
                        <td><?php echo $row["image"]; ?></td>  
                        <td><?php echo $row["m_type"]; ?></td>  
                    </tr>  
<?php  

}

?>  
                </tbody>  
            </table>      
<?php  

$sql = "SELECT COUNT(id) FROM registration";  
$rs_result = mysqli_query($conn, $sql);  
$row = mysqli_fetch_row($rs_result);  
$total_records = $row[0];  
$total_pages = ceil($total_records / $limit);  
$pagLink = "<nav><ul class='pagination'>";  

for ($i=1; $i<=$total_pages; $i++) {                 
    $pagLink .= "<li><a href='index.php?page=".$i."'>".$i."</a></li>";  
} 

echo $pagLink . "</ul></nav>";  
?>
        </div>
    </body>
    <script type="text/javascript">
    $(document).ready(function(){
        $('.pagination').pagination({
            items: <?php echo $total_records;?>,
            itemsOnPage: <?php echo $limit;?>,
            cssStyle: 'light-theme',
            currentPage : <?php echo $page;?>,
            hrefTextPrefix : 'index.php?page='
        });
    });
    </script>
</html>

我需要设置名称&#39; utf8&#39;在这个mysqli_query中,它包含一些用于连接数据库的变量,如何在数据库的html页面中为回显malayalam提供utf 8名称?这是用于使我的数据分页的代码,因为我有大量数据要显示在表中

2 个答案:

答案 0 :(得分:1)

把它放在头上:

mb_internal_encoding("UTF-8");

在mysql连接之后:

mysql_query("set names 'utf8'");

在你的htaccess文件中:

AddDefaultCharset utf8

答案 1 :(得分:0)

在mysqli_connect之后,执行mysqli_set_charset($conn,'utf8mb4');,然后在将其打印到html之前,html对其进行编码。这是我多年来使用的html编码函数:

/**
 * convert any string to valid HTML, as losslessly as possible, assuming UTF-8
 *
 * @param string $str           
 * @return string
 */
function hhb_tohtml(string $str): string {
    return htmlentities ( $str, ENT_QUOTES | ENT_HTML401 | ENT_SUBSTITUTE | ENT_DISALLOWED, 'UTF-8', true );
}

应用它看起来像:

                    <td><?php echo hhb_tohtml($row["id"]); ?></td>  
                    <td><?php echo hhb_tohtml($row["image"]); ?></td>  
                    <td><?php echo hhb_tohtml($row["m_type"]); ?></td>  

编辑:在另一个字符集中存储在mysql数据库中的(不太可能的)事件中,首先使用mb_convert_encoding将其编码为utf8,如mb_convert_encoding($str,'UTF-8','ISO-8859-1') - 或iconv:iconv('ISO-8859-1','UTF-8',$str)