简体   繁体   中英

How Can You Use Both SQL COUNT And mysqli_stmt_num_rows At The Same Time?

I am trying to build a php script where you feed it your email address on the webform. If the email address does not exist in mysql db then the register() function should trigger. Else the login() function. Now, no matter what email address I input, be it one that exists in db or one that doesn't, I always get the register() triggering. Why is that?

Here is the basic code. I have not given the regsiter() or login() codes as they are irrelevant at this point.

if($_SERVER['REQUEST_METHOD'] == 'POST')
{
if(!isset($_POST['email_account']) || !isset($_POST['email_service']))
{
    $email_error = "<font color='red'>Input Email Address!</color>";
}
else
{
    echo "Line 13 triggered<br>"; //DELETE THIS

    $conn = mysqli_connect("localhost","root","","test");

    if($conn === false)
    {
        die("ERROR: Connection Error!. " . mysqli_connect_error());
    }
    else
    {
        echo "Line 24 triggered<br>"; //DELETE THIS
        //Set Parameters.
        $email = trim($_POST["email_account"]) . '@' . trim($_POST["email_service"]);
        echo "line 27 triggered: $email<br>";

        $sql_query = "SELECT COUNT(personal_email) FROM users WHERE personal_email = ?";
        $stmt = mysqli_prepare($conn,$sql_query);
        if($stmt == false) 
        {
          // just for debugging for now:
          die("<pre>Prepare failed:\n".mysqli_error($conn)."\n$sql_query</pre>");
        }
        else
        {
            echo "Line 38 triggered<br>"; //DELETE THIS
            mysqli_stmt_bind_param($stmt,'s',$email);

            if(!mysqli_stmt_execute($stmt))
            {
                echo "Line 43 triggered<br>"; //DELETE THIS
                //Close Connection.
                mysqli_close($conn);

                die("Could not mysqli_stmt_execute! Please try again later!");
            }

            echo "Line 50 triggered<br>"; //DELETE THIS
            //if($sql_query == 1)
            if(mysqli_stmt_num_rows($stmt) == 1)
            {
                echo "Line 57 triggered: Function login() will trigger!<br>"; //DELETE THIS
                $_SESSION['session_type'] = 'login';
                login();

            }
            else
            {
                echo "Line 64 triggered: Function register() will trigger!<br>"; //DELETE THIS
                $_SESSION['session_type'] = 'register';
                register();
            }
        }
    }
}
}

I get echoed the following you see in asterisks:

  • Line 13 triggered
  • Line 24 triggered
  • line 27 triggered: realemail@gmail.com
  • Line 38 triggered
  • Line 53 triggered
  • Line 64 triggered: Function register() will trigger!

If it is possible to use both SQL COUNT and php's mysqli_stmt_num_rows($stmt) , then someone kindly show me how to do it. But if otherwise, then kindly show me 2 examples. One with the Sql COUNT and one with the php's mysqli_stmt_num_rows($stmt) function.

NOTE: I actually want to learn how to count rows using the Sql's COUNT. If COUNT = 0, then register(). Else if COUNT = 1, then login(). Here we are counting the presence of the email address in the mysql database.

You don't need mysqli_stmt_num_rows() at all. You already fetch the COUNT() so just use that result in PHP. mysqli_stmt_num_rows() would give you 1 every time anyway. It is the number of rows returned by MySQL and you always ask for one row containing count of matching records in MySQL.

I removed all the wrong mysqli code from your script and added get_result() . I left the rest as it was.

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    if (!isset($_POST['email_account']) || !isset($_POST['email_service'])) {
        $email_error = "<font color='red'>Input Email Address!</color>";
    } else {
        echo "Line 13 triggered<br>"; //DELETE THIS

        mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
        $conn = mysqli_connect("localhost", "root", "", "test");
        $conn->set_charset('utf8mb4'); // always set the charset

        echo "Line 24 triggered<br>"; //DELETE THIS
        //Set Parameters.
        $email = trim($_POST["email_account"]) . '@' . trim($_POST["email_service"]);
        echo "line 27 triggered: $email<br>";

        $sql_query = "SELECT COUNT(personal_email) FROM users WHERE personal_email = ?";
        $stmt = $conn->prepare($sql_query);

        echo "Line 38 triggered<br>"; //DELETE THIS
        $stmt->bind_param('s', $email);

        $stmt->execute();

        $result = $stmt->get_result();

        echo "Line 50 triggered<br>"; //DELETE THIS
        if ($result->fetch_array()[0]) {
            echo "Line 57 triggered: Function login() will trigger!<br>"; //DELETE THIS
            $_SESSION['session_type'] = 'login';
            login();
        } else {
            echo "Line 64 triggered: Function register() will trigger!<br>"; //DELETE THIS
            $_SESSION['session_type'] = 'register';
            register();
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM