Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using REGEXP inside mysqli prepared statement in PHP

I am trying to make a simple search process with (noob) codes like this:

$prep->prepare("SELECT * FROM details WHERE id REGEXP '?'");
$prep->bind_param("s", $search_query);

It gives me this warning:

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

I am guessing it could be because the question mark is also used for RegExp (optional previous character).

Any idea about how to use REGEXP inside prepared statements (without conflicting question marks)?

Thanks.

like image 716
CluelessNoob Avatar asked Jan 17 '26 23:01

CluelessNoob


1 Answers

Take out the single quotes around the ?. Your code should read:

$prep->prepare("SELECT * FROM details WHERE id REGEXP ?");
$prep->bind_param("s", $search_query);

As it stands now, you are passing in one param, but the ? in single quotes is treated as a string, not a parameter marker.

like image 132
elixenide Avatar answered Jan 20 '26 14:01

elixenide



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!