PHP SQL STMT SELECT multiple LIKE ? is it possible?

拥有回忆 提交于 2021-02-05 12:20:29

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!