解决SQL server不支持utf8,php却用utf8的矛盾问题

时间:2023-03-10 04:25:26
解决SQL server不支持utf8,php却用utf8的矛盾问题
function convert2utf8($string)
{
return iconv("gbk","utf-8",$string);
}
function convert2gbk($string)
{
return iconv("utf-8","gbk",$string);
}

当插入数据,或修改数据的时候,把utf-8,转为gbk,存入数据库。

当获取数据的时候,将数据转为utf-8。

这个方法在底层的数据中设计,上层调用即可。

<?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']==){
$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[], $val[]);
}
} //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;
} /**
* 向表中插入数据
* array $adata 数据
* string $table 表名
* int $insterid 是否需要返回插入ID
* @return true or false or int
*/
// @bianding 2013.11.04 更改了pdo中mssql.php的lastInsertId()函数
// @bianding 2013.11.04 经测试 mssql.php中的lastInsertId()函数中的SELECT两种方式都行
function SaveData($adata, $table, $insterid = , $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;
}
} /**
* 删除表中数据
*
* @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;
}
} /**
* 更新表中数据
*
* @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);
}
}