dtc9222 2018-02-08 07:03
浏览 200
已采纳

在数据库查询中使用逗号作为小数分隔符

I've set Swedish locale to use comma as decimal separator. For example it should be 123,45 instead of 123.45.

To test it:

echo 100/3;

It gives desirable result:

33,333333333333 

However when fetching or inserting data in the database using PHP PDO, it does not seem to work. For if I try to update a table row:

$sql = "UPDATE
            table_name
        SET
            column_name = 123,45 //does not work but 123.45 works.
        WHERE   
            row_id = 1
    ";  
$q = $db->prepare( $sql );
$q -> execute();

In the above code I get error if I try to use a comma separator eg. 123,45 but it works if I use dot. Similarly when I select the data

$sql = "SELECT
            column_name
        FROM
            table_name
        WHERE   
            row_id = 1
    ";  

    $q = $db->prepare( $sql );
    $q -> execute();
    $test = $q -> fetch( PDO::FETCH_COLUMN );
    var_dump($test); //string "123.45" 

It will return the result with dot as a decimal separator. ie. 123.45 while I would like 123,45.

What I'm doing wrong? Edit: The column data type is numeric with scale 2.

  • 写回答

3条回答 默认 最新

  • douyo770657 2018-02-08 07:28
    关注

    This would be impossible for MySQL to handle because the comma is also the separator character in a list. Take for example the following query:

    INSERT INTO table (a,b) VALUES(123,45,67)
    

    What does this mean? Are the values 123 and 45.67? Or 123.45 and 67? Could be either one. It would be totally ambiguous.

    I would suggest doing all your business logic in PHP using en_US and changing the locale to Swedish only for output.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。