dongshan8953 2010-11-27 14:00
浏览 401
已采纳

为什么ENUM(“0”,“1”)在Mysql中保存为空字符串?

I have a MYSQL table with an ENUM field named "offset" and some other columns. The field is defined as:

ENUM(0,1), can be NULL, predefined value NULL

Now I have two server. A production server and a development server and the same PHP script used to create and to update the database.

First step: the application create the record witout passing the "offset" in the CREATE query.

Second step: the application ask to the user some data (not the "offset" value), read the row inserted in step one and make an array, update some field (not the "offset" field), create a query in an automated fashion and save the row again with the updated values.

The automated query builder simple read all the field passed in an array and create the UPDATE string.

In both systems I obtain this array:

$values = array(... 'offset' => null);

and convert it in this same query passing the values in the mysql_real_escape_string:

UPDATE MyTable SET values..., `offset` = '' WHERE id = '10';

Now there is the problem. When i launch the query in the production system, the row is saved, in the development system I got an error and the db says that the offset data is wrong without saving the row.

From phpmyadmin when I create the row with the first step, it shows NULL in the offset field. After saving the field in the system which give no errors, it show me an empty string.

Both system are using MySQL 5 but the production uses 5.0.51 on Linux and development use 5.0.37 on Windows.

The questions:

Why one system give me an error an the other one save the field ? Is a configuration difference ?

Why when I save the field which is an enum "0" or "1" it saves "" and not NULL ?

  • 写回答

3条回答 默认 最新

  • dongzou7134 2010-11-27 14:05
    关注

    Why one system give me an error an the other one save the field ? Is a configuration difference ?

    Probably. See below.

    Why when I save the field which is an enum "0" or "1" it saves "" and not NULL ?

    According to the MySQL ENUM documentation:

    The value may also be the empty string ('') or NULL under certain circumstances:

    • If you insert an invalid value into an ENUM (that is, a string not present in the list of permitted values), the empty string is inserted instead as a special error value. This string can be distinguished from a "normal" empty string by the fact that this string has the numeric value 0. ...

      If strict SQL mode is enabled, attempts to insert invalid ENUM values result in an error.

    (Emphasis added.)

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

报告相同问题?

悬赏问题

  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集