John Siniger
John Siniger

Reputation: 885

Select NULL columns without using "IS NULL"

Hello I need to select all the NULL records in my database which have the column called "cliente_pretenece"

The problem is that I can't define it inside the sql query using IS NULL, because I am using different criteria’s for select, here is the select code:

$sql = "SELECT * FROM users WHERE 1";
if (!empty($id)) {
  $sql .= " AND userID = '$id' ";
}
if (!empty($telefono)) {
  $sql .= " AND telefono LIKE '%$telefono%' ";
}
if (!empty($cliente_pretenece)) {
  $sql .= " AND cliente_pretenece = '$cliente_pretenece' ";
}

the column cliente_pretence will always have 3 different states, and the select code from whch the call is coming is:

<select>
<option  value="SDQ">State 1</option>
<option  value="SA">State 2</option>
<option  value="NULL">Nothing</option>
</select>

So any help will be very welcome. Thanks

Upvotes: 0

Views: 48

Answers (1)

Matti Virkkunen
Matti Virkkunen

Reputation: 65116

You have to use IS NULL. Modify your code so that it reacts differently to the NULL value and generates an IS NULL test instead of a comparison.

Also, learn about parametrized queries before you get an SQL injection.

Upvotes: 2

Related Questions