SQL server不支持utf8 php卻用utf8的矛盾問題解決方法
核心代碼
function convert2utf8($string) { return iconv("gbk","utf-8",$string); } function convert2gbk($string) { return iconv("utf-8","gbk",$string); }
當插入數(shù)據(jù),或修改數(shù)據(jù)的時候,把utf-8,轉(zhuǎn)為gbk,存入數(shù)據(jù)庫。
當獲取數(shù)據(jù)的時候,將數(shù)據(jù)轉(zhuǎn)為utf-8。
這個方法在底層的數(shù)據(jù)中設(shè)計,上層調(diào)用即可。
<?php class dao_dao extends Zend_Db_Table { public function returnDb(){ return $db = &$this->getAdapter(); } public function getData($table,$where = false, $order = 'id ASC', $pagesize = false, $offset = false, $count = false, $from = false, $join = false, $group = false) { $db = &$this->getAdapter(); $select = $db->select(); if ($where && is_array($where)) { foreach ($where as $key => $val) { //print_r($where); if($val['type']==1){ $select->where($key, $val['val']); }else{ $select->orwhere($key, $val['val']); } } } if (!$from) $from = '*'; //echo $select."<br/>"; if ($pagesize) { $select->limit($pagesize, $offset); } //echo $select."<br/>"; if (is_array($order)) { foreach ($order as $value) { $select->order($value); } } else { $select->order($order); } //echo $select."<br/>"; $select->from($table, $count ? "COUNT(".$table.".id)" : $from); if (is_array($group)) { foreach ($group as $key => $val) { $select->group($val); } if ($count) { $result = $db->fetchAll($select); //echo $select."<br/>"; return $result; } } else { if ($count) { $result = $db->fetchOne($select); //echo $select."<br/>"; return $result; } } if (is_array($join)) { foreach ($join as $key => $val) { $select->join($key, $val[0], $val[1]); } } //echo $select."<br/>"; //echo $select;exit; $result = $db->fetchAll($select); foreach ($result as $key => $value) { foreach ($value as $key2 => $value2) { $result[$key][$key2] = $this->convert2utf8($value2); } } return $result; } /** * 向表中插入數(shù)據(jù) * array $adata 數(shù)據(jù) * string $table 表名 * int $insterid 是否需要返回插入ID * @return true or false or int */ // @bianding 2013.11.04 更改了pdo中mssql.php的lastInsertId()函數(shù) // @bianding 2013.11.04 經(jīng)測試 mssql.php中的lastInsertId()函數(shù)中的SELECT兩種方式都行 function SaveData($adata, $table, $insterid = 0, $aLog = false) { $db = & $this->getAdapter(); foreach ($adata as $key => $value) { $adata[$key] = $this->convert2gbk($value); } if ($db->insert($table, $adata)) { //var_dump($db->getProfiler()); $insertedID = $db->lastInsertId(); if ($insterid) { return $insertedID; } else { return TRUE; } } else { return false; } } /** * 刪除表中數(shù)據(jù) * * @param string $table 表名 * @param string $where 'id ='.$id 條件 * @return true or false */ function DelData($table, $where, $aLog = false) { $db = & $this->getAdapter(); if ($db->delete($table, $where)) { return TRUE; } else { return FALSE; } } /** * 更新表中數(shù)據(jù) * * @param string $table * @param array $adata * @param string $where 'id ='.$id * @return true or false */ function UpdateData($table, $adata, $cond, $aLog = false) { $db = & $this->getAdapter(); foreach ($adata as $key => $value) { $adata[$key] = $this->convert2gbk($value); } if ($db->update($table, $adata, $cond)) { return TRUE; } else { return false; } } public function clearTable($table) { $db = &$this->getAdapter(); $result = $db->query('TRUNCATE TABLE ' . $table); } public function executeSql($strSql) { $db = &$this->getAdapter(); $result = $db->query($strSql); } function convert2utf8($string) { return iconv("gbk","utf-8",$string); } function convert2gbk($string) { return iconv("utf-8","gbk",$string); } }
sqlserver 建庫指定utf-8 修改庫為utf-8編碼
CREATE DATABASE paas COLLATE Chinese_PRC_CI_AS
GO
ALTER DATABASE paas COLLATE Chinese_PRC_CI_AS
GO
讓ASP和MS SQL SERVER支持UTF-8編碼存儲多國語言文字
近日在ASP+MS SQL存儲UTF-8編碼內(nèi)容的時候,出現(xiàn)亂碼的情況,經(jīng)過查詢發(fā)現(xiàn)要使SQL SERVER支持UTF-8編碼格式,必須做一些修改才可以。
1、確保ASP頁面是UTF-8編碼的,并在ASP頁面頂部聲明中使用<%@ LANGUAGE = VBScript CodePage = 65001%>進行編碼聲明
2、輸出的HTML頁面中聲明字符集:<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
3、在進行URL參數(shù)傳遞的時候,要使用Server.URLEncode()方法進行編碼
4、使用JS進行URL參數(shù)傳遞中文的時候,要使用escape進行編碼
5、在將UTF-8編碼的內(nèi)容存入SQL SERVER數(shù)據(jù)庫中的時候,要存儲的字段必須設(shè)置為NVARCHAR類型,SQL語句要在內(nèi)容前加N表示,如insert into user (name) values (N´&username&´),除id意外的字段都需要加N。
- Windows Server 2019 IIS10.0+PHP(FastCGI)+MySQL環(huán)境搭建教程
- PHP5.6.8連接SQL Server 2008 R2數(shù)據(jù)庫常用技巧分析總結(jié)
- PHP7使用ODBC連接SQL Server2008 R2數(shù)據(jù)庫示例【基于thinkPHP5.1框架】
- PHP連接SQL Server的方法分析【基于thinkPHP5.1框架】
- PHP基于pdo的數(shù)據(jù)庫操作類【可支持mysql、sqlserver及oracle】
- php連接MSsql server的五種方法總結(jié)
- php連接微軟MSSQL(sql server)完全攻略
- windows server 2008/2012安裝php iis7 mysql環(huán)境搭建教程
- PHP連接SQL server數(shù)據(jù)庫測試腳本運行實例
相關(guān)文章
解析centos中Apache、php、mysql 默認安裝路徑
本篇文章是對centos下Apache、php、mysql 默認安裝路徑進行了詳細的分析介紹,需要的朋友參考下2013-06-06常見的四種POST 提交數(shù)據(jù)方式(小總結(jié))
這篇文章主要介紹了常見的四種POST 提交數(shù)據(jù)方式(小總結(jié))的相關(guān)資料,需要的朋友可以參考下2015-10-10如何阻止網(wǎng)站被惡意反向代理訪問(防網(wǎng)站鏡像)
最近有人用小站數(shù)據(jù),利用反向代理技術(shù),做了個小偷站。用戶訪問的是他的網(wǎng)址,但實質(zhì)上內(nèi)容數(shù)據(jù)確是我的,這是一起惡意反向代理事件2014-03-03php array_unique之后json_encode需要注意
php array_unique之后json_encode需要注意的地方,需要的朋友可以參考下。2011-01-01PHP實現(xiàn)將Word文件保存到SQL Server數(shù)據(jù)庫
這篇文章主要介紹了如何利用PHP實現(xiàn)將上傳的Word文件保存到SQL Server數(shù)據(jù)庫,文中的示例代碼講解詳細,需要的可以參考一下2022-02-02