使用php代码将persian值插入表中

时间:2017-12-20 07:34:57

标签: php mysql

我想在表格中插入一个值。但是当我在代码下面运行时,我看到用????显示了这个值。我该如何解决这个问题?

insert.php

<?php
    include "connect.php";
    $query = "INSERT INTO customer_tab (name,phone,address) VALUE ('آزاده','0981245','ایران کرمان')";
    $result = $connect->prepare($query);
    $result->execute();
?>

connect.php

<?php
    $server = "localhost";
    $user = "azadgh";
    $pass = "*******";
    $dbname = "my_db";
    $dsn = "mysql:host=localhost;dbname=$dbname";
    try {
        $connect = new PDO($dsn, $user, $pass);
        $connect->exec("SET character_set_connection = 'UTF8");
        $connect->exec("SET NAMES 'UTF8");
        $connect->exec("SET CHARACTER SET 'utf8'");
    } catch (PDOException $error) {
        echo "unable to connect : " . $error->getMessage();
    }
?>

提前感谢您的帮助。

2 个答案:

答案 0 :(得分:0)

TRy:PDO("mysql:host=localhost;dbname=DB;charset=UTF8");
这是一个有帮助的解决方案:
$pdo->query("SET CHARACTER_SET_RESULTS=NULL");<br> $result = $pdo->query("select * from table");

答案 1 :(得分:0)

我认为数据库名称没有反映在您的代码中。您可以尝试更改下面的代码,

$dbname = "my_db";
$dsn = "mysql:host=localhost;dbname="+$dbname+";

$dsn = "mysql:host=localhost;dbname=my_db;