0

I'm using prepared statement for MySQLi PHP which is all working. However, the application that I'm using this in has 108 different possible statements all very similar that can be run either without condition:

select * from table1

or with

select * from table 1 where user_level = 1)

The question that I'm asking: Is there a way that I can create the statement that covers all possibilities such as

select * from table 1 where user_level = {special input} 

that will give the same outcome as

select * from table1

Otherwise I'm looking at a lot of repetition.

5
  • For what you have described the solution is simple if the variable is empty take all if not use the variable. Commented Jul 13, 2020 at 11:07
  • Have you tried with IFNULL? Commented Jul 13, 2020 at 11:08
  • I am not sure what it has to do with PHP or mysqli. I think your question is about composing SQL query. Commented Jul 13, 2020 at 11:09
  • May be worth looking at Create a dynamic mysql query using php variables. BUT please use prepared statements as the accepted answer doesn't Commented Jul 13, 2020 at 11:09
  • I understand that what I'm trying to stop is me writing out multiple if statements for things like if user level is set or not. what I'm looking for is a statement that can be written so that the same query can be used for multiple statements Commented Jul 13, 2020 at 11:14

1 Answer 1

0

You would use parameter binding. See https://www.php.net/manual/en/mysqli.quickstart.prepared-statements.php.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.