Q:

Converting a normal SQLi statement containing LIKE to a prepared statement

I have the following code that has wildcard LIKE statements in the queries.  My understanding is that the bind parameters need to be declared as variables before the statement and not within.  I've done this (I think) but am not getting a result.  No error is thrown, but the search comes up empty.  Here is the original code:
$query = $_POST['query'];
$userLoggedIn = $_POST['userLoggedIn'];

$names = explode(" ", $query);

//If query contains an underscore, assume user is searching for usernames
if(strpos($query, '_') !== false) 
    $usersReturnedQuery = mysqli_query($con, "SELECT * FROM users WHERE username LIKE '$query%' AND user_closed='no' LIMIT 4");
//If there are two words, assume they are first and last names respectively
else if(count($names) == 2)
    $usersReturnedQuery = mysqli_query($con, "SELECT * FROM users WHERE (first_name LIKE '$names[0]%' AND last_name LIKE '$names[1]%') AND user_closed='no' LIMIT 4");
//If query has one word only, search first names or last names 
else 
    $usersReturnedQuery = mysqli_query($con, "SELECT * FROM users WHERE (first_name LIKE '$names[0]%' OR last_name LIKE '$names[0]%') AND user_closed='no' LIMIT 4");

if($query != ""){

    while($row = mysqli_fetch_array($usersReturnedQuery)) { etc.etc. 

Here is my attempt at prepared statements:
$query = $_POST['query'] . '%';
$userLoggedIn = $_POST['userLoggedIn'];

$names = explode(" ", $query);
$f_name = $names . '[0]%';
$l_name = $names . '[1]%';
$user_closed = 'no';

//If query contains an underscore, assume user is searching for usernames
if(strpos($query, '_') !== false) 
    $usersReturnedQuery = mysqli_prepare($con, "SELECT * FROM users WHERE username=? AND user_closed=? LIMIT 4");
    $usersReturnedQuery->bind_param('ss', $query, $user_closed);
    $usersReturnedQuery->execute();

    $result = $usersReturnedQuery->get_result();
    $usersReturnedQuery->free_result();

//If there are two words, assume they are first and last names respectively
else if(count($names) == 2)
    $usersReturnedQuery = mysqli_prepare($con, "SELECT * FROM users WHERE (first_name=? AND last_name=?) AND user_closed=? LIMIT 4");
    $usersReturnedQuery->bind_param('sss',$f_name, $l_name, $user_closed);
    $usersReturnedQuery->execute();

    $result = $usersReturnedQuery->get_result();
    $usersReturnedQuery->free_result();

//If query has one word only, search first names or last names 
else 
    $usersReturnedQuery = mysqli_prepare($con, "SELECT * FROM users WHERE (first_name=? OR last_name=?) AND user_closed=? LIMIT 4");
    $usersReturnedQuery->bind_param('sss',$f_name, $f_name, $user_closed);
    $usersReturnedQuery->execute();

    $result = $usersReturnedQuery->get_result();
    $usersReturnedQuery->free_result();

if($query != ""){

    while($row = $result->fetch_assoc()) {

Can anyone see where I went wrong?  

A:

The solution is as follows:
$query = $_POST['query'] . '%';
$userLoggedIn = $_POST['userLoggedIn'];

$names = explode(" ", $query);
$f_name = $names[0] . '%';
$f_name2 = $names[0] . '%';
$l_name = $names[1] . '%';
$user_closed = 'no';

//If query contains an underscore, assume user is searching for usernames
if(strpos($query, '_') !== false) {
    $usersReturnedQuery = mysqli_prepare($con, "SELECT * FROM users WHERE username LIKE ? AND user_closed=? LIMIT 4");
    $usersReturnedQuery->bind_param('ss', $query, $user_closed);
    $usersReturnedQuery->execute();

    $result = $usersReturnedQuery->get_result();
    $usersReturnedQuery->free_result();

//If there are two words, assume they are first and last names respectively
}else if(count($names) == 2) {
    $usersReturnedQuery = mysqli_prepare($con, "SELECT * FROM users WHERE (first_name LIKE ? AND last_name LIKE ?) AND user_closed=? LIMIT 4");
    $usersReturnedQuery->bind_param('sss',$f_name, $l_name, $user_closed);
    $usersReturnedQuery->execute();

    $result = $usersReturnedQuery->get_result();
    $usersReturnedQuery->free_result();

//If query has one word only, search first names or last names 
}else {
    $usersReturnedQuery = mysqli_prepare($con, "SELECT * FROM users WHERE (first_name LIKE ? OR last_name LIKE ?) AND user_closed=? LIMIT 4");
    $usersReturnedQuery->bind_param('sss',$f_name, $f_name2, $user_closed);
    $usersReturnedQuery->execute();

    $result = $usersReturnedQuery->get_result();
    $usersReturnedQuery->free_result();
}

if($query != ""){

    while($row = $result->fetch_assoc()) { etc...

Learned something here again, I hope this is useful for others aswell.  

