Jump to content

PHP or MYSQL Converting Empty String to Zero


Ravenman7
Go to solution Solved by gizmola,

Recommended Posts

Any and All,

 

I have a situation which I cannot find an answer for. I'll list the specifics here and state what the issue is.

 

2 configurations, old and new;

 

Old   -    PHP 5.2.6, MySQL 5.1

 

New  -    PHP 5.5.8  MYSQL 5.6

 

On the new setup, the following query throws an error (as it should)

 

INSERT INTO sample set ProjectID='026LC',LabSampleID='1',SampleType='REG',DateReceived='2014-03-05 00:00:00',DateSampled='2014-03-0400:00:00',TimeSampled='0',ClientSampleID='MI1',Media='Water',`Condition`='Intact',

sort=1,ContainerType='',Temperature='',Initial_Amount_Units='',ContainerNum=''

 

ContainerNum is defined as int(10) default null. The error is  - Incorrect integer value: '' for column 'ContainerNum' at row 1

 

On the old setup, no error is thrown. The record is inserted with a zero in the ContainerNum field.

 

INSERT INTO sample set ProjectID='007LC',LabSampleID='1',SampleType='REG',DateReceived='2014-03-05 00:00:00',DateSampled='2014-03-04 00:00:00',TimeSampled='0',ClientSampleID='MI1',Media='Water',`Condition`='Intact',

sort=1,ContainerType='',Temperature='',Initial_Amount_Units='',ContainerNum=''

 

Query Browser shows the ContainerNum field containing a zero as seen in the attached image.

 

post-167911-0-59771700-1394043590_thumb.png

 

 

There are no triggers in the database. Any idea on how this was accomplished? The code is broken is several places on the new server.

 

Rick

 

Link to comment
Share on other sites

  • Solution

Your new server is set to SQL Strict mode. Your old server is not, thus it is downgrading the null to a 0.

 

Take a look at this page: http://dev.mysql.com/doc/refman/5.0/en/sql-mode.html

 

As you can see there are quite a lot of different options available to tweak the behavior of the server.

 

Probably the strict server has something like this in its my.cnf file:

# Set the SQL mode to strict sql-mode="STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"
STRICT_TRANS_TABLES will cause that sort of insert to error out.

 

You will want to diff the mysql configurations on the two servers, and compare the my.cnf for each to pinpoint the specific difference between the two.

 

However, most likely it's the STRICT_TRANS_TABLES setting that explains the difference. On the old server, this setting is not being used, so the server is generating a warning instead, but otherwise creating the row.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.