Jump to content

[SOLVED] syntax question


182x

Recommended Posts

Hey guys,

 

I have an int field in a database and for teh purposes of this case the insert statement doesn't insert anything into the field using the ' ' syntax however in the mysql database this appears as 0. I was wondering can I still use the empty syntax as follows or will it not work as it is 0 does is this not classed as empty? Thanks.

 

  if (empty($row['intTest'])){

 $sns= "empty";
 }

Link to comment
Share on other sites

you could enable a NULL value for the field, and insert NULL rather than an empty string.  this entails a delicate balance of quotes vs. non-quotes, because the NULL value must be inserted into the database without single quotes in order to be properly recognized as a NULL value:

 

INSERT INTO stuff (one, two, integer) VALUES ('first', 'second', NULL)

Link to comment
Share on other sites

Thanks but does that mean the above example would work? I was also wondering is it normal for an integer to be set to zero when in the sql statement the insert for that integer is left blank like using single quotes ' ' ?

Link to comment
Share on other sites

the database will insert the default value for that column when fed an empty string like that.  for columns designated NOT NULL, this will mean a 0 for most numeric fields, or an actual empty string ('') for text or varchar field types.

 

the above example won't work, because "empty" is just a string.  since it's an incorrect type, it will either spring an error (type mismatch) or disregard the value you gave it and insert the default.

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.