Organizational Research By

Surprising Reserch Topic

check if row exists with mysqli


check if row exists with mysqli  using -'php,mysqli'

I need help checking if a row exists. I am getting "email no longer exists publisher@example.com".

Is there a better way to check if row exists with mysqli?

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

    $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";
    }   
}

    

asked Oct 13, 2015 by tseetha
0 votes
54 views



Related Hot Questions

2 Answers

0 votes

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).

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

if(mysqli_num_rows($query) > 0){

    echo "email already exists";
}else{
    // do something
    if (!mysqli_query($con,$query))
    {
        die('Error: ' . mysqli_error($con));
    }
}

and in your case:

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

if(mysqli_num_rows($query) > 0){

    echo "email already exists";
}else{
    // do something
    if (!mysqli_query($dbl,$query))
    {
        die('Error: ' . mysqli_error($dbl));
    }
}

or:

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

            }
        }
    }

And a PDO method:

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.";
    }


    }
?>
answered Oct 13, 2015 by sanjaypal1983
0 votes

You have to execute your query and add single quote to $email in the query beacuse it's a string, and remove the is_resource($query) $query is a string, the $result will be the resource

$query = "SELECT `email` FROM `tblUser` WHERE `email` = '$email'";
$result = mysqli_query($link,$query); //$link is the connection

if(mysqli_num_rows($result) > 0 ){....}

UPDATE

Base in your edit just change:

if(is_resource($query) && mysqli_num_rows($query) > 0 ){
        $query = mysqli_fetch_assoc($query);
        echo $email . " email exists " .  $query["email"] . "\n";

By

if(is_resource($result) && mysqli_num_rows($result) == 1 ){
        $row = mysqli_fetch_assoc($result);
         echo $email . " email exists " .  $row["email"] . "\n";

and you will be fine

UPDATE 2

A better way should be have a Store Procedure that execute the following SQL passing the Email as Parameter

SELECT IF( EXISTS (
                  SELECT *
                  FROM `Table`
                  WHERE `email` = @Email)
          , 1, 0) as `Exist`

and retrieve the value in php

Pseudocodigo:

 $query = Call MYSQL_SP($EMAIL);
 $result = mysqli_query($conn,$query);
 $row = mysqli_fetch_array($result)
 $exist = ($row['Exist']==1)? 'the email exist' : 'the email doesnt exist';
answered Oct 13, 2015 by dahiyabecomp

...