472,796 Members | 1,507 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,796 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 2969
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...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth

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.