Amy Neville
Amy Neville

Reputation: 10611

MySqli LIKE query not matching number of parameter

The following query is resulting in the browser printing a

"does not match number of parameters"

type of error.

Why is this happening?

When I replace with LIKE '%".$country."%' and get rid of the bind_param it does not bring up any errors.

$query = "
SELECT * from (    
    SELECT link
    FROM items
    WHERE countries LIKE '%?%'
    ORDER BY value DESC
    LIMIT 10   
) T ORDER BY RAND() 
LIMIT 1
";
if ($statement = $mysqli->prepare($query))
{
    $statement->bind_param("s", $country);
    $statement->execute();
    $statement->store_result();
    $statement->bind_result($link);
    $statement->fetch();
    $statement->free_result();
    $statement->close();
}

I'd like to prepare the statement instead of inserting raw data into the query.

Upvotes: 0

Views: 93

Answers (2)

Saty
Saty

Reputation: 22532

You can use it like

$country = "%{$country}%";

$query = "
SELECT * from (    
    SELECT link
    FROM items
    WHERE countries LIKE ?
    ORDER BY value DESC
    LIMIT 10   
) T ORDER BY RAND() 
LIMIT 1
";
if ($statement = $mysqli->prepare($query))
{
    $statement->bind_param("s", $country);
    $statement->execute();
    $statement->store_result();
    $statement->bind_result($link);
    $statement->fetch();
    $statement->free_result();
    $statement->close();
}

Upvotes: 1

Jens
Jens

Reputation: 69460

% must be part of the value :

$query = "
SELECT * from (    
    SELECT link
    FROM items
    WHERE countries LIKE ?
    ORDER BY value DESC
    LIMIT 10   
) T ORDER BY RAND() 
LIMIT 1
";
if ($statement = $mysqli->prepare($query))
{
    $statement->bind_param("s","%".$Country."%");
    $statement->execute();
    $statement->store_result();
    $statement->bind_result($link);
    $statement->fetch();
    $statement->free_result();
    $statement->close();
}

Upvotes: 1

Related Questions