• Thinkphp5 PDO操作mysql预处理中文字段出错问题


    Thinkphp5手册上建议不用中文表明和中文字段名

    今天发现中文字出问题的地方了

    $pdo = new PDO('mysql:host=localhost;dbname=xsfm_master', 'root','root' );
    $pre = $pdo->prepare('UPDATE `ep购买`  SET `支付宝`=:data__111  WHERE  `id` = :where_id  ');
    $arr = [':data__111'=>'852',':where_id'=>'1'];
    $pre->execute($arr);

    中文字段是完全支持的

    官网是这么说的

    http://www.thinkphp.cn/topic/44305.html

    但是 Thinkphp5中绑定是这么干的

    "UPDATE `ep购买`  SET `支付宝`=:data__支付宝  WHERE  `id` = :where_id  "
      ["data__支付宝"] => array(2) {
        [0] => string(3) "998"
        [1] => int(2)
      }
      ["where_id"] => array(2) {
        [0] => int(1)
        [1] => int(1)
      }

    测试一下果然出错

    $pre = $pdo->prepare('UPDATE `ep购买`  SET `支付宝`=:data__支付宝  WHERE  `id` = :where_id  ');
    $arr = [':data__支付宝'=>'852',':where_id'=>'1'];
    $pre->execute($arr);

    知道错误原因就容易解决了

    我是这么干的

    Thinkphp5.1 hinkphplibrary hinkdbBuilder.php

    P:103

    elseif (is_scalar($val)) {
                    // 过滤非标量数据
                    if (0 === strpos($val, ':') && $query->isBind(substr($val, 1))) {
                        $result[$item] = $val;
                    } else {
                        $key = str_replace('.', '_', $key);
                        if (preg_match("/[x7f-xff]/", $key)) {    //add_判断字符串中是否有中文
                            $query->bind('data__' . md5($key), $val, isset($bind[$key]) ? $bind[$key] : PDO::PARAM_STR);//add
                            $result[$item] = ':data__' . md5($key);  //add
                        } else {                      //add
                            $query->bind('data__' . $key, $val, isset($bind[$key]) ? $bind[$key] : PDO::PARAM_STR);
                            $result[$item] = ':data__' . $key;
                        }                           //add
                    }
                }

     公司有人改这里用的是 base64_encode方法

    想到Thinkphp5 Model返回的都是implement ArrayAccess 的对象,虽然可以以数组的方式访问,但是作者建议用$obj->id的方式访问,如果中文字段 $obj->数量,不优雅

    20180112

    thinkphp5.1.3我是这么干的

  • 相关阅读:
    BZOJ 3343 教主的魔法 分块
    HDU 3118 Arbiter 判定奇圈
    Atitit. Attilax企业框架 AEF的发展里程总结
    HDU 2009 整除的尾数 题解
    多态的应用《植物大战僵尸》
    Unity3D调用摄像头显示当前拍摄画面
    oracle下session的查询与删除
    为easyUI的dataGrid加入自己的查询框
    JQuery EasyUI Combobox 实现省市二级联动菜单
    hadoop 2.4.1 集群安装二
  • 原文地址:https://www.cnblogs.com/8000cabbage/p/7476917.html
Copyright © 2020-2023  润新知