This question already has an answer here:
OK so my question is I have a function but i want to parse it different WHERE clauses for when it executes a query. for example:
function query($where)
{
$query = $mysql->prepare("SELECT * FROM table WHERE ?");
$query->bind_param("s", $where);
$query->execute();
...
}
query("table.id=123 AND table.name='abc'");
I have learnt that this is incorrect So how do I perform something similar, i have many places where I need to use this function with different WHERE clauses, and make a function for each is impractical and so is not making a function and calling it directly.
Different queries in an
if
(or if you have a lot of variances,switch
) statement:Overly generified obviously, but you should get the general idea. If you know that there will always be the same number of values, and it will always be the same type, you can simplify it:
For a prepared statement with a WHERE clause you have to specify what values will be specified later, for instance:
if you want to make it more dynamic you can have specify the query in one function and then call the query function. For example, you have this:
and in your other function you say:
you could make this even more sophisticated by making a query class that can contain an array of where clauses:
and change your query function to look like: