There is a very easy way to do this.
create this prepared statement:
select * from mytable where status = ? and (userid = ? or ?) and (location = ? or ?) order by `date` desc, time desc limt ?
and pass args to bind as follows:
$stmt = $mysqli->prepare( [statement above] ); $stmt->bind_param( "siiiii", "active", $userid, $userid == "ALL", $location, $location == "ALL", $limit);
The predicate (user_id = ? or ?) Will be true if user_id is equal to the first replaced parameter or when the second replaced parameter is true.
$user_id when converted to int will be its value when it is a string representation of a number, or zero otherwise. The expression $userid == "ALL" will be evaluated before the boolean, which will be passed to bind_param . We cannot tell bind_param that the parameter is logical (the format string understands only the string, int, double and blob), so bind_param converts the boolean value into int, which works for us.
As long as there is no user_id or location_id in the database, you are fine.
tpdi
source share