php - serialize()包括类型一并序列化,如果存在数字或数字字符串时如何做两个系统间的相等判断
伊谢尔伦
伊谢尔伦 2017-04-11 10:06:09
0
2
803

有遇到过类似从MySQL数据库中,不需要用特定的类型转化,比如无需操作CAST和CONVERT就能得到查询的结果集全部是字符串的情况吗?

/**
 * 获取系统中该月结算单数据
 *
 * @param $from
 * @param $to
 * @return array|CDbDataReader|mixed
 */
public function getTargetData($from, $to)
{
    $res = $this->targetDb->createCommand("
        SELECT
            platform,
            commission_product_total_amount,
            bill_month,
            refund_total_amount,
            fee_settlement,
            anchor_source,
            fee_adjust_amount,
            commission_product_number,
            refund_sku_number,
            bill_sn,
            buyer_number,
            commission_sku_total_amount,
            fee_total_amount,
            anchor_uid,
            anchor_nickname
        FROM `{$this->targetTable}`
        WHERE bill_month = " . date('Ym', strtotime($from)) . "
    ")->queryAll();
    $this->changeType($res);

    return $res;
}

目前是做一次循环,觉得太过多余

/**
 * 因接口返回值问题,造成数据类型不一致,比如返回的金额为字符或浮点格式,
 * 但是mysql存储为整型等,故将mysql查询后的数据全部转为字符串
 *
 * @param array $list
 */
protected function changeType(&$list = [])
{
    foreach ($list as &$value) {
        foreach ($value as $key => $val) {
            $value[$key] = strval($val);
        }
    }
}
伊谢尔伦
伊谢尔伦

小伙看你根骨奇佳,潜力无限,来学PHP伐。

reply all(2)
Peter_Zhu

用PDO的话:PDO::ATTR_STRINGIFY_FETCHES,例:

$pdo = new PDO('...');
$pdo->setAttribute(PDO::ATTR_STRINGIFY_FETCHES, true);
$stm = $pdo->prepare("select 1, 2.3, 'hello'");
$stm->execute();
$result = $stm->fetch(PDO::FETCH_NUM);
左手右手慢动作

Yii1框架是可以直接设置PDO属性的

$this->targetDb->setAttributes([PDO::ATTR_STRINGIFY_FETCHES, true]);

然后继续查询就可以得到完全的字符串描述,当然不般是不会有这种需求的,不推荐强制转化字段的类型。

[0]=>
  array(15) {
    ["platform"]=>
    string(6) "聚美"
    ["commission_product_total_amount"]=>
    string(7) "3843.23"
    ["bill_month"]=>
    string(6) "201701"
    ["refund_total_amount"]=>
    string(7) "1552.06"
    ["fee_settlement"]=>
    string(6) "192.16"
    ["anchor_source"]=>
    string(0) ""
    ["fee_adjust_amount"]=>
    string(4) "0.00"
    ["commission_product_number"]=>
    string(2) "22"
    ["refund_sku_number"]=>
    string(1) "8"
    ["bill_sn"]=>
    string(14) "JS201701000001"
    ["buyer_number"]=>
    string(2) "13"
    ["commission_sku_total_amount"]=>
    string(7) "2291.17"
    ["fee_total_amount"]=>
    string(6) "192.16"
    ["anchor_uid"]=>
    string(9) "108619065"
    ["anchor_nickname"]=>
    string(0) ""
  }
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template