Query search for value in Database

Asked

Viewed 856 times

2

I want to show the results obtained in a form but when saving in the comic null but kept so:

Date: 0000-00-00 And Numero: 0

Query have so:

$sql = mysql_query ("Select *, Count(*) from tabela1, tabela2 where campo1 = 0 and campo2= '0000-00-00' and campo3 is Null and campo4......'")

But he’s not showing me the result. I have something wrong with the code?

  • What is the format/type of the column?

  • Put it like this and see if any errors appear $sql = mysql_query('select....') or (die mysql_error())

  • Date is in Date the number is Int

  • @user3253195, already tested this direct query in the database?

  • Mysql Error does not show me anything.

  • I must have an error in the way to ask for values. Because in the BD query also shows blank.

  • 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......'")

  • 2

    Edit the question and place exactly the query you are trying to execute.

  • @user3253195 Click on link to the chat suggested by lost to see more information so we can help you.

  • If possible post on SQL Fiddle, will facilitate to answer

  • 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

Show 7 more comments

3 answers

5


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:

Captura de Tela

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:

Captura de Tela


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:

Captura de Tela

1

I figured it out. Although it appears in the database 0000-00-00 on the date I put like this: I thank those who helped me :)

include("conectar.php");
$sql = mysql_query("SELECT * FROM tb_trabalhador WHERE AlvaraNumero = 0 
 AND(AlvaraValidade='' or AlvaraValidade is Null or AlvaraValidade='0000-00-00')
 AND (AlvaraAnexo='' or AlvaraAnexo is Null)
 AND AcidenteNumero = 0
 AND (AcidenteValidade='' or AcidenteValidade is Null or AcidenteValidade='0000-00-00')    
 AND (AcidenteAnexo='' or AcidenteAnexo is Null)
 AND SeguroNumero = 0
 AND (SeguroValidade='' or SeguroValidade is Null or SeguroValidade='0000-00-00' )
 AND (SeguroAnexo='' or SeguroAnexo is Null)
 AND InstaladorNumero = 0
 AND (InstaladorValidade='' or InstaladorValidade is Null or InstaladorValidade='0000-00-00')
 AND (InstaladorAnexo='' or InstaladorAnexo is Null) " ) or DIE( mysql_error());

 while($exibe = mysql_fetch_array($sql)){ 

0

Name the tables to show in which table the field you pass by "WHERE".

Ex:

$sql = mysql_query ("SELECT *, COUNT(*) FROM tabela1 AS tbl1, tabela2 AS tbl2 WHERE tbl1.campo1 = 0 and tbl2.campo2= '0000-00-00' and tbl1.campo3 is Null and tbl2.campo4......'");
  • I just call a table and it shows me blank. But if I do select * from Tabela1 It shows me the entered data

Browser other questions tagged

You are not signed in. Login or sign up in order to post.