I'm trying to insert FALSE value in the database, without any luck - it inserts an empty string. The column is varchar. If I insert TRUE value, it inserts 1.
I'm using mysql, PDO and php.
I'm also using a DB class which does nothing more with the query than preparing and executing it using PDO prepared statements.
The query in mind is a very basic one:
$sql = 'INSERT INTO api_logs_values (value) VALUES (?)';
and then:
$this->_db->query($sql, array(FALSE));
any ideas what is happening?
EDIT:
Added table structure:
api_logs_values | CREATE TABLE `api_logs_values` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`log_id` int(10) unsigned NOT NULL,
`name` varchar(255) DEFAULT NULL,
`value` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `log_id` (`log_id`,`name`)
ENGINE=MyISAM AUTO_INCREMENT=26 DEFAULT CHARSET=utf8
EDIT2:
I've found a bug report concerning this here https://bugs.php.net/bug.php?id=33876, but there doesn't seem to be a proper, nice solution to this. Except maybe setting the PDO_PARAM_BOOL attribute.
EDIT3:
I've found out that the DB class that I am using does not bind the params separately, but does execute($param), and looking here http://php.net/manual/en/pdostatement.execute.php, it treats all params as PDO::PARAM_STR, which is not correct.
For example to insert NULL instead of an empty string using NULLIF() , we could do the following: INSERT INTO `user` (`name`, `job_title`) VALUES ('john', NULLIF('$jobTitle', '')); This would insert NULL in the job_title column when the value of the variable " $jobTitle " matches an empty string.
You use NULL : insert into table tablename values (12, 13, 19, NULL, NULL, NULL, NULL, 1, NULL, 2, NULL, 5); You can also use '' if the column is a string and by "blank" you mean "empty string".
Learn MySQL from scratch for Data Science and AnalyticsIS NULL − This operator returns true, if the column value is NULL. IS NOT NULL − This operator returns true, if the column value is not NULL. <=> − This operator compares values, which (unlike the = operator) is true even for two NULL values.
You can insert NULL value into an int column with a condition i.e. the column must not have NOT NULL constraints. The syntax is as follows. INSERT INTO yourTableName(yourColumnName) values(NULL);
If you want to insert the string "false", then $this->_db->query($sql, array("FALSE"));
For datatype varchar
, php value of false is considered as empty string.
If you want TRUE
to be 1
, and FALSE
to be 0
, set the datatype of the column to tinyint
.
MySQL will insert values that evaluate to false into columns. Depending on the datatype the actual value will differ. For character-based datatypes, an empty string will be inserted. For numerical types it will insert 0. You should use an appropriate data type. (FYI, while MySQL recognises the keyword BOOL for datatype definitions, the column will actually be created as TINYINT. In this case it will store 1 for true and 0 for false)
If you want to literally store the string "false" then you need to use a string literal instead of boolean false.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With