Arabic character “???????” on php and mysql

2019-08-31 03:29发布

问题:


here is my problem, i have an arabic database mysqli UTF8_general_ci and the charset of my php file is UTF-8 when i seled data i get "?????????????".
on php my admin i can write and read with no problem, also on php i can, but the problem is when is when i get data from the database! here is my code:

<meta charset="UTF-8"/>

$connexion = new mysqli(HOST_DB,USER_DB,MDP_DB,DB_NAME);
        if ($connexion->error) {
            die('Une erreur s\'est produite :' .$connexion->error);
        }
        else{
            $sql = "SELECT nom, description, date, formateur, image, prix, lieux FROM news";
            if ($result = mysqli_query($connexion,$sql)) {
                while ($ligne = mysqli_fetch_assoc($result)) {
                    echo'
                        <ul>
                            <li><figure>
                                <figcaption>
                                    <h1>'.$ligne["nom"].'</h1>
                                    <p>'.$ligne["description"].'</p>
                                    <strong>اليوم:'.$ligne["date"].'</strong><br/>
                                    <strong>المكان:'.$ligne["lieux"].'</strong><br/>
                                    <strong>المدرس:'.$ligne["formateur"].'</strong><br/>
                                    <strong>الثمن: '.$ligne["prix"].'دينار</strong>
                                    </figcaption>
                                <img src="Images/'.$ligne["image"].'" height="250px" width="250px">
                            </figure></li>
                        </ul>
                    ';
                }
            }
        }

回答1:

put this code after the connection to mysql. mysqli_set_charset($connexion,'utf8'); this is final code for connection to database: $connexion = new mysqli(HOST_DB,USER_DB,MDP_DB,DB_NAME); mysqli_set_charset($connexion,'utf8');



回答2:

<?php header('Content-Type: text/html; charset=utf-8'); ?>

will modify the header to your encoding



回答3:

Run this SQL query:

SET NAMES 'utf-8'


回答4:

The problem is from XAMPP, i swiched to Wamp server and rebooted the machine and all work fine,
Thanks ALL



回答5:

it will work you have to change my.ini file inside mysql\bin and add the below line

character-set-server = utf8



标签: php mysql mysqli