简体   繁体   English

如何同时使用 SQL COUNT 和 mysqli_stmt_num_rows?

[英]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.我正在尝试构建一个 php 脚本,您可以在其中将您的 email 地址提供给它。 If the email address does not exist in mysql db then the register() function should trigger.如果 email 地址在 mysql db 中不存在,则 register() function 应该触发。 Else the login() function.否则 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.现在,无论我输入什么 email 地址,无论是 db 中存在还是不存在,我总是得到 register() 触发。 Why is that?这是为什么?

Here is the basic code.这是基本代码。 I have not given the regsiter() or login() codes as they are irrelevant at this point.我没有给出 regsiter() 或 login() 代码,因为此时它们无关紧要。

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第 13 行触发
  • Line 24 triggered第 24 行触发
  • line 27 triggered: realemail@gmail.com第 27 行触发:realemail@gmail.com
  • Line 38 triggered第 38 行触发
  • Line 53 triggered第 53 行触发
  • Line 64 triggered: Function register() will trigger!第 64 行触发:Function register() 将触发!

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.如果可以同时使用 SQL COUNT 和 php 的mysqli_stmt_num_rows($stmt) ,那么请有人告诉我该怎么做。 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.一个是 Sql COUNT,一个是 php 的mysqli_stmt_num_rows($stmt) function。

NOTE: I actually want to learn how to count rows using the Sql's COUNT.注意:我实际上想学习如何使用 Sql 的 COUNT 来计算行数。 If COUNT = 0, then register().如果 COUNT = 0,则 register()。 Else if COUNT = 1, then login().否则,如果 COUNT = 1,则 login()。 Here we are counting the presence of the email address in the mysql database.这里我们计算 mysql 数据库中 email 地址的存在。

You don't need mysqli_stmt_num_rows() at all.你根本不需要mysqli_stmt_num_rows() You already fetch the COUNT() so just use that result in PHP.您已经获取了COUNT() ,所以只需在 PHP 中使用该结果。 mysqli_stmt_num_rows() would give you 1 every time anyway. mysqli_stmt_num_rows()每次都会给你 1。 It is the number of rows returned by MySQL and you always ask for one row containing count of matching records in MySQL.它是 MySQL 返回的行数,您总是要求一行包含 MySQL 中匹配记录的计数。

I removed all the wrong mysqli code from your script and added get_result() .我从您的脚本中删除了所有错误的 mysqli 代码并添加了get_result() I left the rest as it was.我留下了 rest 原样。

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();
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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