往数据库插入中文的时候,显示乱码

用php往数据库插入中文的时候,显示乱码的解决方法,就是在建立与数据库的连接之后,加上这一句话:

mysqli_query($this->link,"set names 'utf8'");

在php中,建立与数据库的连接的方法如下:

    /**
     * 连接数据库
     */
    private function conn()
    {
        // require(dirname(__FILE__).'/../config.php');
        global $dbname;
        global $host;
        global $user;
        global $pwd;
        $db_name = $dbname;
        $db_encode = 'utf8';
        $this->prefix = 'weixin_';
        if (isset(M::$wlink)) {         
            # code...
            $this->link = M::$wlink;
            // echo "<br>use old link ";
        }else{
            $this->link = mysqli_connect($host,$user,$pwd,$db_name);        //与数据库建立连接
            // echo "<br>use new link :".M::$wlink;
        }
        
	if (!$this->link){                                                    //判断是否与数据库建立连接
		echo "Error: Unable to connect to MySQL." . PHP_EOL;
		echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL;
		echo "Debugging error: " . mysqli_connect_error() . PHP_EOL;

		die("Connection error: " . mysqli_connect_error());
	}
        M::$wlink = $this->link;
        mysqli_query($this->link,"set names 'utf8'");  //这句话用来解决在数据库插入中文显示乱码的问题
     
    }


相关文章

文章浏览阅读8.4k次,点赞8次,收藏7次。SourceCodester Onl...
文章浏览阅读3.4k次,点赞46次,收藏51次。本文为大家介绍在...
文章浏览阅读1.1k次。- php是最优秀, 最原生的模板语言, 替代...
文章浏览阅读1.1k次,点赞18次,收藏15次。整理K8s网络相关笔...
文章浏览阅读1.2k次,点赞22次,收藏19次。此网络模型提供了...
文章浏览阅读1.1k次,点赞14次,收藏19次。当我们谈论网络安...