473,385 Members | 1,676 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

mysql and null values

Hello, quick question about MySQL storing NULL values...

Say I have a textbox called $_POST["text"] and a variable $var.

if(empty($_POST["text"]))
$var = NULL;
else
$var = $_POST["text"];

Disregarding filtering/formatting the data, upon inserting $var into
MySQL, I thought it *should* result in a NULL entry in the database (the
field is setup to accept NULL values). I am sure $var = NULL before
inserting into the table because is_null() returns TRUE.

The field is of type CHAR and the entry gets added simply as ''
(obviously without the quotes). If I run a select statement to find all
entries where field = NULL, I get 0, but where field = '' returns the entry.

I don't understand why it's doing this. The only way I can get it to say
NULL in the record is by actually setting $var = 'NULL', but that to the
best of my knowledge is just setting $var to the CHAR value 'NULL', not
actually NULL.

Thanks in advance for all your help.

Marcus
Jul 17 '05 #1
1 3009
lig
I beleive the main problem is that MySQL has a special syntax when
setting something to NULL. Simply assigning it will not work. I would
suggest you look here -
http://dev.mysql.com/doc/mysql/en/pr...with-null.html . Maybe in
the PHP you should set it to the string 'NULL' and then in the SQL make
sure it is not quoted.

quote from the manual:
* If you want to search for column values that are NULL, you cannot use
an
* expr = NULL test. The following statement returns no rows, because
* expr = NULL is never true for any expression:
*
* mysql> SELECT * FROM my_table WHERE phone = NULL;
*
* To look for NULL values, you must use the IS NULL test. The following

* statements show how to find the NULL phone number and the empty phone
number:
*
* mysql> SELECT * FROM my_table WHERE phone IS NULL;
* mysql> SELECT * FROM my_table WHERE phone = '';

Marcus wrote:
Hello, quick question about MySQL storing NULL values...

Say I have a textbox called $_POST["text"] and a variable $var.

if(empty($_POST["text"]))
$var = NULL;
else
$var = $_POST["text"];

Disregarding filtering/formatting the data, upon inserting $var into
MySQL, I thought it *should* result in a NULL entry in the database (the field is setup to a ccept NULL values). I am sure $var = NULL before
inserting into the table because is_null() returns TRUE.

The field is of type CHAR and the entry gets added simply as ''
(obviously without the quotes). If I run a select statement to find all entries where field = NULL, I get 0, but where field = '' returns the entry.
I don't understand why it's doing this. The only way I can get it to say NULL in the record is by actually setting $var = 'NULL', but that to the best of my knowledge is just setting $var to the CHAR value 'NULL', not actually NULL.

Thanks in advance for all your help.

Marcus


Jul 17 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Simon | last post by:
Hi, I am having a little problem with my PHP - MySQl code, I have two tables (shown below) and I am trying populate a template page with data from both. <disclaimer>Now I would like to say my...
0
by: Neculai Macarie | last post by:
Hi! Using Union and Order By gives strange behaviour in the following test-case: drop table if exists gallery; drop table if exists gallery_categ; # create test tables create table gallery...
2
by: Asfand Yar Qazi | last post by:
Ahem.. Anyway, here's whats happening... construct tables in MySQL: DROP TABLE EMP; CREATE TABLE EMP (EMPNO INT(4) NOT NULL, ENAME CHAR(6) NOT NULL,
0
by: Mike Chirico | last post by:
Interesting Things to Know about MySQL Mike Chirico (mchirico@users.sourceforge.net) Copyright (GPU Free Documentation License) 2004 Last Updated: Mon Jun 7 10:37:28 EDT 2004 The latest...
1
by: Saqib Ali | last post by:
I have created 2 tables in my MySQL database. A_TAB and B_TAB. They have auto-incrementing integer primary keys respectively named A_ID & B_ID. When I created B_TAB, I declared a field named A_ID...
10
by: Python_it | last post by:
Python 2.4 MySQL-python.exe-1.2.0.win32-py2.4.zip How can I insert a NULL value in a table (MySQL-database). I can't set a var to NULL? Or is there a other possibility? My var must be variable...
6
Atli
by: Atli | last post by:
This is an easy to digest 12 step guide on basics of using MySQL. It's a great refresher for those who need it and it work's great for first time MySQL users. Anyone should be able to get...
1
ssnaik84
by: ssnaik84 | last post by:
Hi Guys, Last year I got a chance to work with R&D team, which was working on DB scripts conversion.. Though there is migration tool available, it converts only tables and constraints.. Rest of...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.