182x Posted August 1, 2007 Share Posted August 1, 2007 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"; } Quote Link to comment https://forums.phpfreaks.com/topic/62903-solved-syntax-question/ Share on other sites More sharing options...
akitchin Posted August 1, 2007 Share Posted August 1, 2007 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) Quote Link to comment https://forums.phpfreaks.com/topic/62903-solved-syntax-question/#findComment-313125 Share on other sites More sharing options...
182x Posted August 1, 2007 Author Share Posted August 1, 2007 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 ' ' ? Quote Link to comment https://forums.phpfreaks.com/topic/62903-solved-syntax-question/#findComment-313128 Share on other sites More sharing options...
akitchin Posted August 1, 2007 Share Posted August 1, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/62903-solved-syntax-question/#findComment-313138 Share on other sites More sharing options...
182x Posted August 1, 2007 Author Share Posted August 1, 2007 Thanks, I can't test this as I am not at a mchine with PHP but I just found this link which indicates that it should work, is it wrong? Thanks again http://uk2.php.net/empty Quote Link to comment https://forums.phpfreaks.com/topic/62903-solved-syntax-question/#findComment-313141 Share on other sites More sharing options...
akitchin Posted August 1, 2007 Share Posted August 1, 2007 the empty() function should work - i meant trying to insert "empty" will still result in a 0 for the integer column. i guess i misunderstood your question. Quote Link to comment https://forums.phpfreaks.com/topic/62903-solved-syntax-question/#findComment-313145 Share on other sites More sharing options...
182x Posted August 1, 2007 Author Share Posted August 1, 2007 Ah i see yes I wanted it to insert 0 then check for it using empty so this can be achieved using the above methods of the empty insert ' ' and the empty() function? Quote Link to comment https://forums.phpfreaks.com/topic/62903-solved-syntax-question/#findComment-313148 Share on other sites More sharing options...
akitchin Posted August 1, 2007 Share Posted August 1, 2007 yes. it will come up as empty whether an integer or a string when it comes out of the database. Quote Link to comment https://forums.phpfreaks.com/topic/62903-solved-syntax-question/#findComment-313151 Share on other sites More sharing options...
182x Posted August 1, 2007 Author Share Posted August 1, 2007 Thanks for your help. Quote Link to comment https://forums.phpfreaks.com/topic/62903-solved-syntax-question/#findComment-313162 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.