Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to bind a parameter inside of a LIKE clause with percentages in mysqli? [duplicate]

Tags:

php

mysqli

I'm using mysqli, and I have this query:

$q = "SELECT col1, col2 FROM `Something` WHERE col2 LIKE '%?%'";

The same query works fine if I query it outside of PHP with a word in place of ?. Within PHP, I can't do this. The error I'm getting is:

mysqli_stmt::bind_param(): Number of variables doesn't match number of parameters in prepared statement[..]

The query is outputting fine in var_dump(), as is the parameter itself.

Here's the bind_param():

$stmt->bind_param("s", $param);

What am I doing wrong?

like image 943
Mark Buffalo Avatar asked Sep 13 '25 18:09

Mark Buffalo


1 Answers

It's just a matter of placement of the signs

$q = "SELECT col1, col2 FROM `Something` WHERE col2 LIKE ?";

$stmt->bind_param("s", '%' . $param . '%');

I know this has thrown off a lot of people, you're not the first :)

like image 97
JimL Avatar answered Sep 16 '25 07:09

JimL