In your question, you are indicating that the difficulty is in the operation of inserting new lines in the database. After that, you have a query to collect results and you indicate that the query is not working as intended, let’s analyze:
- How to create the table so that things work as you want;
- Perform data insertion;
- Collect existing records.
Create the table
How do you intend the columns with the name campo1
and campo2
are null
by default, you have to give this indication in the table itself:
As you can see, we are indicating that the default value for the column campo1
and campo2
is null
.
The code for creating the table would be:
CREATE TABLE IF NOT EXISTS `tabela1` (
`id` int(13) NOT NULL AUTO_INCREMENT,
`campo1` int(13) DEFAULT NULL,
`campo2` date DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;
Insert results
With the table properly prepared to accept null
in the columns campo1
and campo2
, the insertion consultation would be:
INSERT INTO `tabela1` ( `id` , `campo1` , `campo2` )
VALUES (NULL , NULL , NULL);
This will insert a type row into the table:
Hold consultations
Now that the table is in the desired form, the queries looking for records null
can already be realized.
Same query as in the question, that is, the column campo1
equal to 0
and the column campo2
equal to null
or equal to 0000-00-00
:
SELECT *
FROM `tabela1`
WHERE `campo1` =0
AND (
`campo2` IS NULL
OR `campo2` = '0000-00-00'
)
That returns us precisely two records where both are with the value in the column campo1
equal to 0
and the value of the column campo2
equal to NULL
in ID 1 and equal to 0000-00-00
in ID 3:
What is the format/type of the column?
– Sergio
Put it like this and see if any errors appear
$sql = mysql_query('select....') or (die mysql_error())
– rray
Date is in Date the number is Int
– ChrisAdler
@user3253195, already tested this direct query in the database?
– rray
Mysql Error does not show me anything.
– ChrisAdler
I must have an error in the way to ask for values. Because in the BD query also shows blank.
– ChrisAdler
I asked to display the date 0000-00-00 or null and still nothing. $sql = mysql_query ("Select , Count() from Tabela1, table 2 Where (field 1 = 0 or field 1 is NULL) and (field 2= '0000-00-00' or field 2 is NULL) and field 3 is Null and field 4......'")
– ChrisAdler
Edit the question and place exactly the query you are trying to execute.
– rray
let’s go continue this discussão in chat
– rray
@user3253195 Click on link to the chat suggested by lost to see more information so we can help you.
– Zuul
If possible post on SQL Fiddle, will facilitate to answer
– Maicon Carraro
Only to complement the PHP 5.5.x version the mysql extension is discontinued. PHP net recommends using Mysqli or Pdo_mysql >"The original Mysql Extension is now deprecated, and will generate E_DEPRECATED >errors when Connecting to a database. Instead, use the Mysqli or Pdo_mysql >Extensions." Source http://www.php.net/manual/en/migration55.deprecated.php
– Marcos Xavier