Store Arabic text in mysql database using php

后端 未结 4 1169
终归单人心
终归单人心 2020-12-17 05:00

I am trying to store some Arabic data in a mysql database I have set the html document charset to be \'utf8\'



        
相关标签:
4条回答
  • 2020-12-17 05:44

    Weird, It should work!

    Try adding these lines after a successful connection to the database:

    $mysqli->query("SET NAMES 'utf8'");
    $mysqli->query("SET CHARACTER SET utf8");
    


    For example:

    $mysqli = @new mysqli('DB_HOST', 'DB_USER', 'DB_PASS', 'DB_NAME');
    
    if($mysqli->connect_errno) die('Connection Error!');
    else{
        $mysqli->query("SET NAMES 'utf8'");
        $mysqli->query("SET CHARACTER SET utf8");
    }
    


    And include these META tags in the head section of your php page:

    <meta charset="utf-8">
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    
    0 讨论(0)
  • 2020-12-17 05:44

    save this file in ur website, htdocs folder as "dbh.php, then in include it at the top of your index page by typing <?php include 'dbh.php';?> and you u are ready to go.

    <?php
    
    $conn = mysqli_connect("localhost", "root", "", "database name");
    
    if(!$conn){
        die("connection failed: ".mysqli_connect_error());
    }else{
        $conn->query("SET NAMES 'utf8'");
        $conn->query("SET CHARACTER SET utf8");
    
    }
    
    0 讨论(0)
  • 2020-12-17 05:47

    You may try this. It should work

    <?php
    
    $conn = mysqli_connect(<host>, <user name>, <password>, <database name>);
    
    if(!$conn){
        die("could not connect: ".mysqli_connect_error());
    }else{
        mysqli_query("SET character_set_results = 'utf8', character_set_client = 'utf8', character_set_connection = 'utf8', character_set_database = 'utf8', character_set_server = 'utf8'", $conn);
    }
    ?>
    
    0 讨论(0)
  • 2020-12-17 06:00

    Browsers will ignore the <meta http-equiv ...> tag if there is a http header present. Try putting this at the top of your php page:

    header("Content-Type: text/html; charset=UTF-8");
    
    0 讨论(0)
提交回复
热议问题