问题
SELECT *
FROM datatable
WHERE Name LIKE ?
OR Code LIKE ?
OR Date LIKE ?
OR Inserter LIKE ?
AND ID = '2'
There is an error in php sql connection: mysqli_stmt::bind_param(): Number of variables doesn't match number of parameters in prepared statement in
Here is the error code, I am using
stmt->prepare, stmt->execute
and so on in php.
This works fine with just one LIKE ? (WHERE Name LIKE ? AND ID = '2').
How can I replace or solve this problem?
The real code
if ($stmt = $db->prepare($SearchQuery)) {
// Bind variables to the prepared statement as parameters
$stmt->bind_param("s", $param_term);
// Set parameters $param_term = '%' . $_POST["String"] . '%';
// Attempt to execute the prepared statement
if ($stmt->execute()) {
$result = $stmt->get_result();
回答1:
You have 4 ?
parameters and you are binding only one value
$stmt->bind_param("s", $param_term);
This should be
$stmt->bind_param("ssss", $param_term,
$param_term,
$param_term,
$param_term);
so that each parameter ?
gets a value.
Also the query will need bracket to work correctly like this
WHERE (Name LIKE ?
OR Code LIKE ?
OR Date LIKE ?
OR Inserter LIKE ? )
AND ID = '2'
回答2:
If you want to transfer strictly one copy of parameter into the query (assuming that you search the same pattern) you may use
SELECT *
FROM datatable
WHERE CONCAT_WS(CHAR(X), Name,Code,Date,Inserter) LIKE ?
AND ID = '2'
where CHAR(x) is any char which cannot be found in a field or in a pattern.
来源:https://stackoverflow.com/questions/59784814/php-sql-stmt-select-multiple-like-is-it-possible