“set names'utf8'”有$变量

<?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
", 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>

I need to set names 'utf8' in this mysqli_query it contains some variable which is used to connect the database, how can i give that utf 8 name for echoing malayalam in my html page from database? this is the code i used for making my data paginated because i am having bulk of data to be displayed in the table

after mysqli_connect, do mysqli_set_charset($conn,'utf8mb4');, and before printing it out to the html, html encode it. here's the html encode function i have used for years:

/**
 * 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 );
}

applying it would look like:

                    <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>  

edit: in the (unlikely) event that it's stored in the mysql database in another characterset, use mb_convert_encoding to encode it to utf8 first, like mb_convert_encoding($str,'UTF-8','ISO-8859-1') - or iconv: iconv('ISO-8859-1','UTF-8',$str)

Put it in head:

mb_internal_encoding("UTF-8");

After mysql connect:

mysql_query("set names 'utf8'");

In your htaccess file:

AddDefaultCharset utf8