简体   繁体   中英

Prepared statement error in PHP

I have the following SQL query that is executed using PHP, $q is a variable with a string in it.

$sql =$conn->prepare('SELECT * FROM namebase WHERE name LIKE "%?%"');
$sql->bind_param('s', $q);
$sql->execute();

While executing, I receive an error message saying,

Warning: mysqli_stmt::bind_param(): Number of variables doesn't match number of parameters in prepared statement in C:\xampp\htdocs\file\s.php on line 39

Line 39 is $sql->bind_param('s', $q);

What is causing this error? I am only passing one variable to the query. Then why is it showing this error message?

Take a look at the comments on the documentation page for the mysqli_stmt::bind_param method, someone has come across exactly the same problem: https://secure.php.net/manual/en/mysqli-stmt.bind-param.php#102048

I had a problem with the LIKE operator

This code did not work:

 <?php $test = $sql->prepare("SELECT name FROM names WHERE name LIKE %?%"); $test->bind_param("s", $myname); ?> 

The solution is:

 <?php $test = $sql->prepare("SELECT name FROM names WHERE name LIKE ?"); $param = "%" . $myname . "%"; $test->bind_param("s", $param); ?> 

If you have access to the database I would suggest creating a Stored Procedure.

And then create as follows:

getNames(
    IN nameVAR VARCHAR(256)
)

SELECT * FROM namebase WHERE name LIKE ‘%nameVAR%’;

Then in your php call getName(?) instead of putting your current statement

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM