• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
INSERTing a NULL value into a database

#1
[eluser]TheFuzzy0ne[/eluser]
Hi everyone. How do I add a NULL value into a database table? Using PHP's NULL causes an error, using an empty string inserts, well, an empty string, and using 'NULL' inserts a string.

Many thanks in advance.

EDIT: It would appear that I've answered my own question. PHP NULL is converted by the active record class when inserting and updating, however, I don't know how to SELECT a row WHERE a specific field equals NULL. This throws a database error telling me the WHERE clause is invalid

Code:
SELECT *
FROM table
WHERE field !=
LIMIT 1;

#2
[eluser]Dam1an[/eluser]
Have you set it to allow and default to null? Then its a simple case of doing nothingm, is it not?

#3
[eluser]TheFuzzy0ne[/eluser]
Good morning, Dam1an. Yes, I have, but I think I was asking the wrong question. Please see my edit above.

#4
[eluser]rogierb[/eluser]
Hi Fuzzy,

Code:
insert into `table` (fieldname, another_field_name) values (NULL, 'abcd')

Should work if the field is allowed a NULL value.
I don't think this can be doen trough active record, but could be mistaken.


[edit]: ah, post is changed so above doesn't apply

#5
[eluser]rogierb[/eluser]
You cannot use arithmetics with NULL, but you can use 'is'

Code:
select * from table where fieldname is NULL

#6
[eluser]cindor[/eluser]
hi!

you can easily get NULL values by IS_NULL operator in the query like this:

SELECT * FROM table WHERE value IS NULL

edit: ahh damn a little late Smile

#7
[eluser]Colin Williams[/eluser]
... which should look like

Code:
$this->db->where('field IS', NULL)

And if that doesn't work out, AR needs an update.

#8
[eluser]cindor[/eluser]
i guess you can write like this

Code:
$this->db->where->('field is NULL')

#9
[eluser]TheFuzzy0ne[/eluser]
Fantastic. Thanks a lot everyone.


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.