0

I have a query that selects based on multiple things. For example, game_name is a string that can be empty if the user so chooses. Is there any way to not do the searching part for game_name if the input is "" but still look for the others?

 $stmt = $connection->prepare("SELECT * FROM $config->tablename WHERE id = ? OR game_name = ? OR gamer_tag = ? LIMIT ?");
 $stmt->bind_param("ssss", $request->id, $request->game_name, $request->gamer_tag, $request->limit);

2 Answers 2

1

You should do a little workaround to control it and when the user wants the field to be empty, you should control that and add to query something like this:

convert this

game_name = ?

into this

game_name = ? OR game_name = game_name

So this will make always to be true for that field.

EDIT: Combining @Amado's solution and my proposal resolves in less IF statements (only my POV).

$MyWhere = "id = ? OR gamer_tag = ? OR game_name = ?"
if(game_name !="") $MyWhere = $MyWhere." OR game_name = game_name";
$stmt = $connection->prepare("SELECT * FROM $config->tablename WHERE ".$MyWhere." LIMIT ?");
$stmt->bind_param("ssss", $request->id, $request->gamer_tag, $request->game_name, $request->limit);
Sign up to request clarification or add additional context in comments.

Comments

1

You can add a little checking before performing the query:

    $MyWhere = "id = ? OR gamer_tag = ?"
    if(game_name !="") $MyWhere = $MyWhere." Or game_name = ?";
    $stmt = $connection->prepare("SELECT * FROM $config->tablename WHERE ".$MyWhere." LIMIT ?");
if(game_name !=""){
       $stmt->bind_param("ssss", $request->id, $request->game_name, $request->gamer_tag, $request->limit);
}
else{
    $stmt->bind_param("sss",$request->id,$request->gamer_tag, $request->limit);
}

8 Comments

How affect parameter binding when you have 3 params and binding 4 params?
I can not edit because is a very little issue: In else there is "ssss" and should be "sss" to fit with the number of params
check answer again
I understand what you want to do. At first sight "else $MyGameName = ?;" will raise an error, but I'm ont sure. I cannot check if it works, but being "prepared statements", what do you expect passing "?" to statement? an always true for that field?.
You have just returned to your intial point. With this code you need again an IF-ELSE to use three or four bindind paramenters like you did in your first attempt. "sss" if for when you have three "?" and "ssss" is for when you have four "?" It's like a mapping. The number of "s" must match with the number of "?". Initial $MyWhere has two "?", if(game_name !="") Will become into three "?". All this goes into prepare statement, which has another "?". So you will need to bind three or four "?"
|

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.