Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

empty value inserted when parameter is false

Tags:

php

mysql

pdo

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.

like image 714
donk Avatar asked Jan 17 '12 08:01

donk


People also ask

How insert NULL instead of empty string?

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.

How to insert empty value in MySQL?

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".

How to handle NULL values in MySQL?

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.

How do you insert a NULL in SQL?

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);


2 Answers

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.

like image 57
xdazz Avatar answered Sep 25 '22 23:09

xdazz


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.

like image 41
GordonM Avatar answered Sep 22 '22 23:09

GordonM