如何检查MySQL中是否存在一行?(即检查 MySQL 中是否存在电子邮件) [英] How to check if a row exists in MySQL? (i.e. check if an email exists in MySQL)

查看:32
本文介绍了如何检查MySQL中是否存在一行?(即检查 MySQL 中是否存在电子邮件)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要帮助检查数据库中是否存在一行.就我而言,该行包含一个电子邮件地址.我得到了结果:

I need help checking if a row exists in the database. In my case, that row contains an email address. I am getting the result:

email no longer exists publisher@example.com

这是我目前使用的代码:

This is the code I'm currently using:

if (count($_POST)) {
    $email = $dbl->real_escape_string(trim(strip_tags($_POST['email'])));

    $query = "SELECT `email` FROM `tblUser` WHERE `email` = '$email'";
    $result = mysqli_query($dbl, $query);
    if (is_resource($result) && mysqli_num_rows($result) == 1) {
        $row = mysqli_fetch_assoc($result);
        echo $email . " email exists " .  $row["email"] . "\n";
    } else {
        echo "email no longer exists" . $email . "\n";
    }
}

是否有更好的方法来检查 MySQL 中是否存在一行(在我的情况下,检查 MySQL 中是否存在电子邮件)?

Is there a better way to check if a row exists in MySQL (in my case, check if an email exists in MySQL)?

推荐答案

以下是经过尝试、测试和验证的方法,用于检查行是否存在.

The following are tried, tested and proven methods to check if a row exists.

(其中一些是我自己用的,或者过去用过的).

(Some of which I use myself, or have used in the past).

我在语法中犯了一个错误,我使用了两次 mysqli_query().请查阅修订版.

I made an previous error in my syntax where I used mysqli_query() twice. Please consult the revision(s).

即:

if (!mysqli_query($con,$query)) 应该简单地读为 if (!$query).

  • 我很抱歉忽略了那个错误.

旁注: '".$var."''$var' 都做同样的事情.您可以使用任何一种,两者都是有效的语法.

Side note: Both '".$var."' and '$var' do the same thing. You can use either one, both are valid syntax.

以下是两个已编辑的查询:

Here are the two edited queries:

$query = mysqli_query($con, "SELECT * FROM emails WHERE email='".$email."'");

    if (!$query)
    {
        die('Error: ' . mysqli_error($con));
    }

if(mysqli_num_rows($query) > 0){

    echo "email already exists";

}else{

    // do something

}

在你的情况下:

$query = mysqli_query($dbl, "SELECT * FROM `tblUser` WHERE email='".$email."'");

    if (!$query)
    {
        die('Error: ' . mysqli_error($dbl));
    }

if(mysqli_num_rows($query) > 0){

    echo "email already exists";

}else{

    // do something

}

您还可以将mysqli_ 与准备好的语句一起使用 方法:

You can also use mysqli_ with a prepared statement method:

$query = "SELECT `email` FROM `tblUser` WHERE email=?";

if ($stmt = $dbl->prepare($query)){

        $stmt->bind_param("s", $email);

        if($stmt->execute()){
            $stmt->store_result();

            $email_check= "";         
            $stmt->bind_result($email_check);
            $stmt->fetch();

            if ($stmt->num_rows == 1){

            echo "That Email already exists.";
            exit;

            }
        }
    }

或带有准备好的语句的 PDO 方法:

<?php
$email = $_POST['email'];

$mysql_hostname = 'xxx';
$mysql_username = 'xxx';
$mysql_password = 'xxx';
$mysql_dbname = 'xxx';

try {
$conn= new PDO("mysql:host=$mysql_hostname;dbname=$mysql_dbname", $mysql_username, $mysql_password); 
     $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
     exit( $e->getMessage() );
}

// assuming a named submit button
if(isset($_POST['submit']))
    {

        try {
            $stmt = $conn->prepare('SELECT `email` FROM `tblUser` WHERE email = ?');
            $stmt->bindParam(1, $_POST['email']); 
            $stmt->execute();
            while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {

            }
        }
        catch(PDOException $e) {
            echo 'ERROR: ' . $e->getMessage();
        }

    if($stmt->rowCount() > 0){
        echo "The record exists!";
    } else {
        echo "The record is non-existant.";
    }


    }
?>

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆