php处理特殊字符

时间:2022-11-02 20:06:39
php怎样把英文名字中的点号,保存到数据库中?如:戴维·卡梅伦
我之前试着保存过,但不成功。只能保存“戴维”,后面的内容就丢失了。
还有一个问题,“黄粿”这个词,保存到数据库的时候,也是只保存个“黄”字,后面的内容就丢失了。

请问高人,这个问题怎么解决?
我的页面编码,数据库的编码,都设置的是UTF-8。

16 个解决方案

#1


用UTF-8不会出现这样的问题

#2


长度是否不够?

#3


页面代码

<tr>
                        <td width="20%">客户信息:</td>
                        <td>
                            <textarea id="customermsg" name="customermsg" cols="90" rows="5" style="border: 1px solid; border-color: #4DA0F4; margin-top: 10px; margin-right: 10px;"></textarea>
                        </td>
                    </tr>

接收数据的代码:

$text=$_POST["customermsg"];
    $accountId=$_POST["accountid"];
    $time=date("Y-m-d H:i:s");
    //获取用户id
    session_start();
    $userid=$_SESSION["uid"];

    $information=new Information();
    $information->setUId($userid);
    $information->setAId($accountId);
    $information->setContent(addslashes(nl2br(trim($text))));
    $information->setTimes($time);

    $bool=$information->addInformation($information);

保存数据的代码:

public function addInformation(Information $infor){
        include_once '../DAO/ConnectionDB.php';

        ConnectionDB::connectDB();
        try{
            mysql_query("set names gb2312");
            $sql="insert into information (uId,aId,content,times) values ('$infor->uId','$infor->aId','$infor->content','$infor->times')";
            $bool=mysql_query($sql);
        }catch(Exception $e) {
            echo $e->getTraceAsString();
            echo iconv('utf-8','gb2312','添加信息失败!');
        }
        
        return $bool;
    }

#4


数据库中,该字段的类型是longtext

#5


页面编码,数据库的编码,都设置的是UTF-8
为什么执行 mysql_query("set names gb2312");

#6


复制表结构出来,确认编码先

#7


表结构:
CREATE TABLE `information` (
  `id` int(11) NOT NULL auto_increment,
  `uId` char(160) NOT NULL,
  `aId` char(160) NOT NULL,
  `content` longtext NOT NULL,
  `times` datetime NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=59 DEFAULT CHARSET=utf8;

#8


引用 5 楼 heyli 的回复:
页面编码,数据库的编码,都设置的是UTF-8
为什么执行 mysql_query("set names gb2312");


LZ看这个。。。UTF-8不显示字符,一定是你代码问题

另外,从 echo iconv('utf-8','gb2312','添加信息失败!');貌似,你的页面编码,不是你所说的utf-8

#9


,,php文档时utf-8,但是网站前台是GBK吧?

#10


如果我把mysql_query("set names gb2312");注释掉。保存到数据库后,会乱码

#11


 mysql_query("set names gb2312");
让俺看着蛋疼!

#12


要用 gbk 而不是 gb2312

#13


请高人来帮帮忙!

#14


mysql_query("set names 'GBK'");

#15


頂貼收藏。

#16


utf8和utf-8有什么区别?

#1


用UTF-8不会出现这样的问题

#2


长度是否不够?

#3


页面代码

<tr>
                        <td width="20%">客户信息:</td>
                        <td>
                            <textarea id="customermsg" name="customermsg" cols="90" rows="5" style="border: 1px solid; border-color: #4DA0F4; margin-top: 10px; margin-right: 10px;"></textarea>
                        </td>
                    </tr>

接收数据的代码:

$text=$_POST["customermsg"];
    $accountId=$_POST["accountid"];
    $time=date("Y-m-d H:i:s");
    //获取用户id
    session_start();
    $userid=$_SESSION["uid"];

    $information=new Information();
    $information->setUId($userid);
    $information->setAId($accountId);
    $information->setContent(addslashes(nl2br(trim($text))));
    $information->setTimes($time);

    $bool=$information->addInformation($information);

保存数据的代码:

public function addInformation(Information $infor){
        include_once '../DAO/ConnectionDB.php';

        ConnectionDB::connectDB();
        try{
            mysql_query("set names gb2312");
            $sql="insert into information (uId,aId,content,times) values ('$infor->uId','$infor->aId','$infor->content','$infor->times')";
            $bool=mysql_query($sql);
        }catch(Exception $e) {
            echo $e->getTraceAsString();
            echo iconv('utf-8','gb2312','添加信息失败!');
        }
        
        return $bool;
    }

#4


数据库中,该字段的类型是longtext

#5


页面编码,数据库的编码,都设置的是UTF-8
为什么执行 mysql_query("set names gb2312");

#6


复制表结构出来,确认编码先

#7


表结构:
CREATE TABLE `information` (
  `id` int(11) NOT NULL auto_increment,
  `uId` char(160) NOT NULL,
  `aId` char(160) NOT NULL,
  `content` longtext NOT NULL,
  `times` datetime NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=59 DEFAULT CHARSET=utf8;

#8


引用 5 楼 heyli 的回复:
页面编码,数据库的编码,都设置的是UTF-8
为什么执行 mysql_query("set names gb2312");


LZ看这个。。。UTF-8不显示字符,一定是你代码问题

另外,从 echo iconv('utf-8','gb2312','添加信息失败!');貌似,你的页面编码,不是你所说的utf-8

#9


,,php文档时utf-8,但是网站前台是GBK吧?

#10


如果我把mysql_query("set names gb2312");注释掉。保存到数据库后,会乱码

#11


 mysql_query("set names gb2312");
让俺看着蛋疼!

#12


要用 gbk 而不是 gb2312

#13


请高人来帮帮忙!

#14


mysql_query("set names 'GBK'");

#15


頂貼收藏。

#16


utf8和utf-8有什么区别?