如果找到数据,则显示文本

时间:2016-05-11 09:28:25

标签: php

以下代码将显示"已找到电子邮件"如果数据库中存在电子邮件地址,但如果数据库中不存在数据,则不显示任何内容。

我无法显示"找不到电子邮件"如果它不存在。

你能看出出了什么问题吗?

<?php
if (isset($_POST['submit'])) {
    error_reporting(E_ALL);
    $email = $_POST['email'];
    include($_SERVER["DOCUMENT_ROOT"]."/dbconnect.php");
    $sql    = "SELECT * FROM membership_main WHERE email = '$email' ";
    $result = mysqli_query($conn, $sql);

    while($rows = mysqli_fetch_assoc($result)){
        if(mysqli_num_rows($result) == 0) {
            echo "email was not found";
        } else {
            echo "email was found";
        }
    }

    mysqli_close($conn);
}
?>

3 个答案:

答案 0 :(得分:1)

你可以尝试这段代码......

<?php

if (isset($_POST['submit'])) {

    error_reporting(E_ALL);

    $email = $_POST['email'];

    include($_SERVER["DOCUMENT_ROOT"] . "/dbconnect.php");

    $sql = "SELECT * FROM membership_main WHERE email = '$email' ";

    $result = mysqli_query($conn, $sql);

    if (mysqli_num_rows($result) > 0) {
        echo "email was found";
    } else {
        echo "email was not found";
    }


    mysqli_close($conn);
}
?>

答案 1 :(得分:1)

你应该将代码移到

之外
while($rows=mysqli_fetch_assoc($result)){
if(mysqli_num_rows($result)==0) {
echo "email was not found";
} else {
echo "email was found";
}
}

if(mysqli_num_rows($result)==0) {
echo "email was not found";
} else {
echo "email was found";
}

答案 2 :(得分:0)

删除while循环,然后查看mysqli_num_rows()

数据存在后使用while循环。

<?php

if (isset($_POST['submit'])) {

error_reporting(E_ALL);

$email = $_POST['email'];

include($_SERVER["DOCUMENT_ROOT"]."/dbconnect.php");

$sql  = "SELECT * FROM membership_main WHERE email = '$email' ";

$result = mysqli_query($conn, $sql);

if(mysqli_num_rows($result) == 0) {
echo "email was not found";
} else {
 while($row = mysqli_fetch_assoc($result))
 {
    echo $row['email']." email was found";
 }
}

mysqli_close($conn);

}

?>