首页 > 解决方案 > Mysql搜索条件为两行

问题描述

在表中搜索时引入了一个条件

 $sql = "SELECT id,email  FROM employee WHERE email LIKE ?"; 

在代码中需要添加条件id='12'

更多代码请查看以下代码

if(isset($_REQUEST["term"])){
// Prepare a select statement
$sql = "SELECT id,email FROM employee WHERE id='12' AND email LIKE ?";

if($stmt = mysqli_prepare($link, $sql)){
    // Bind variables to the prepared statement as parameters
    mysqli_stmt_bind_param($stmt, "s", $param_term);

    // Set parameters
    $param_term = '%'. $_REQUEST["term"] . '%';

    // Attempt to execute the prepared statement
    if(mysqli_stmt_execute($stmt)){
        $result = mysqli_stmt_get_result($stmt);

        // Check number of rows in the result set
        if(mysqli_num_rows($result) > 0){
            // Fetch result rows as an associative array
            while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){
                echo "<p>" . $row["email"] . "</p>";
            }
        } else{
            echo "<p>No matches found</p>";
        }
    } else{
        echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
    }
}

// Close statement
mysqli_stmt_close($stmt);

}

标签: phpmysqli

解决方案


试试这个

$sql = "SELECT id,email  FROM employee WHERE id='12' AND email LIKE ?";

更改代码

// Set parameters first of all
$param_term = '%'. $_REQUEST["term"] . '%';
if($stmt = mysqli_prepare($link, $sql)){
// Bind variables to the prepared statement as parameters
mysqli_stmt_bind_param($stmt, "s", $param_term);

推荐阅读