Mysql поиск с условием двух строк - PullRequest
0 голосов
/ 27 января 2019

при поиске в таблице введено условие

 $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);

}

Ответы [ 3 ]

0 голосов
/ 27 января 2019

просто пытается

$sql = "SELECT id,email FROM employee WHERE email = ? AND id = '12' "; 
0 голосов
/ 27 января 2019

Вам нужно изменить количество и тип параметров, которые вы привязываете к вашему запросу.

if(isset($_REQUEST["term"])){
$id_param = 12; // Parameter One - ID - INT
$param_term = $_REQUEST['term'] . "%"; // Parameter Two - EMAIL LIKE - STRING
// Prepare a select statement
$sql = "SELECT id,email FROM employee WHERE id = ? AND email LIKE ?";

if($stmt = mysqli_prepare($link, $sql)){
    // Bind variables to the prepared statement as parameters
    mysqli_stmt_bind_param($stmt, "is", $id_param, $param_term); //The IS represents INT then STRING  types to be expected, and you follow it up by providing them in that order.

    // 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);
}
0 голосов
/ 27 января 2019

Попробуйте это

$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);
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...