收集和自己做TP+MSSQL经验分享一

浏览:1925 发布日期:2013/07/17 分类:技术分享 关键字: mssql
1、插入数据库数据为乱码
ThinkPHP/Lib/Think/Db/Db.class.php下:
修改insert方法:
 public function insert($data,$options=array())
           $values[]   =  $this->conv2gbk($value); //自定义转码操作for mssql
              //$values[]   =  $value;                  //原操作
    /**
     * utf-8 to gb2312 编码转换 for mssql
     * 自定义方法
     */
  public function conv2utf8($string)
     {
        return iconv(“gb2312″,”utf-8″,$string);
     }
  public  function conv2gbk($string)
     {
        return iconv(“utf-8″,”gb2312″,$string);
     }
2、查找数据为乱码
ThinkPHP/Lib/Think/Db/Db.class.php下:
添加如下方法:
    /**
     * gb2312 to utf-8 数组方式编码转换 for mssql
     * 自定义方法
     */
     public function conv2utf8Array($Row){
        //$ResultSet = array();
          $RowSet = array();
        foreach($Row as $key=>$RowPerValue){
        $RowSet[$key] = iconv(“gb2312″,”utf-8″,$RowPerValue);
        }
        return $RowSet;
     }
ThinkPHP\Lib\Think\Db\Driver\DbMssql.class.php下:
修改getAll()方法
  $result[] = $this->conv2utf8Array($row);   //为mssql实现转码
              //$result[]   =   $row;                    //原操作
3、findall()方法执行只查询出一条语句:
MSSQL驱动的paresLimit函数的问题.
public function parseLimit($limit) {
if(empty($limit)) $limit=1;
改成if(empty($limit)) return ’1=1′;
4、save()修改数据不成功:
a\Model.class.php:
// 主键名称
    protected $pk  = ‘Id’;
Id为数据表主键名称(严格区分大小写)
b\Db.class.php:
将order、limit注释
 * 更新记录
     +———————————————————-
     * @access public
     +———————————————————-
     * @param mixed $data 数据
     * @param array $options 表达式
     +———————————————————-
     * @return false | integer
     +———————————————————-
     */
    public function update($data,$options) {
           //.$this->parseOrder(isset($options['order'])?$options['order']:”)
           //.$this->parseLimit(isset($options['limit'])?$options['limit']:”)

5、save()修改数据后乱码解决问题:
Db.class.php:添加函数
 /**
     * utf-8 to gb2312 数组方式编码转换 for mssql
     * 自定义方法
     */
     public function conv2gbkArray($Row){
        //$ResultSet = array();
          $RowSet = array();
        foreach($Row as $key=>$RowPerValue){
        $RowSet[$key] = iconv(“utf-8″,”gb2312″,$RowPerValue);
        }
        return $RowSet;
     }
Model.class.php:添加转码代码
public function save($data=”,$options=array()) {
// 数据转码
        $data = $this->db->conv2gbkArray($data);
6、删除不成功问题解决:
Db.class.php:
注释limit、order
    /**
     +———————————————————-
     * 删除记录
     +———————————————————-
     * @access public
     +———————————————————-
     * @param array $options 表达式
     +———————————————————-
     * @return false | integer
     +———————————————————-
     */
    public function delete($options=array())

           // .$this->parseOrder(isset($options['order'])?$options['order']:”)
           // .$this->parseLimit(isset($options['limit'])?$options['limit']:”)
注意:
比如计算用户积分的时候,mysql的时候会写:
data['score'] = array('exp','score+1');// 用户的积分加1
但是使用mssql的时候,最好写成:
$data['login_count'] = $authInfo['login_count']+1;

以上是收集和个人遇到并解决的,欢迎大牛指导。
最佳答案
评论( 相关
后面还有条评论,点击查看>>