If I declare a variable in a SELECT query, is it mandatory?

2

My problem is that I will not always receive the variables, so I need a way that does not make them mandatory even by declaring them, for example in the query below:

"SELECT * FROM clientes WHERE clientes.nome = :nome";

Var :nome is required;

Then I need that even if the variable :nome does not contain a value, the query returns me all the clients

    
asked by anonymous 01.07.2017 / 03:42

2 answers

2

I recommend doing this directly to php.

You can use if to do this, as Stéfano suggested, or in a simpler way:

$sql = empty($nome) ? "SELECT * FROM clientes" : "SELECT * FROM clientes WHERE clientes.nome = :nome";
    
01.07.2017 / 04:24
4

Verify with IF .

if(!empty($nome)){
$where = "clientes.nome = :nome";
} else {$where = "";}

$query = "SELECT * FROM clientes WHERE $where";

You can edit the else to add other rules in where .

    
01.07.2017 / 04:14