OO MYSQLI num_rows always = -1



我只想检查用户名是否已经存在于我的注册页面数据库中,要做到这一点,我想我可以用输入的用户名做一个选择查询,看看我得到多少结果。但是,num_rows总是= -1我使用的测试用户名在数据库中两次,所以当我输入它时,它应该转到else并打印"这个用户已经存在"。我对mysqli也有点陌生,所以如果你看到任何错误,我将非常感谢你的反馈。

我的PHP在这里:

            <?php
            if(isset($_POST['submit'])){
                    $connection = new mysqli("localhost","username","password","DB");
                    if($connection->connect_errno){
                            echo "Failed to connecto MYSQL: (" . $connection->connect_errno . ") " . $connection-> connect_error;
                    }
                    else{
                            $username = $_POST['username'];
                            $password = $_POST['password'];
                            $password = password_hash($password, PASSWORD_DEFAULT);
                            $check_exist = $connection->prepare("SELECT username FROM users WHERE username = ?");
                            $check_exist->bind_param('s', $username);
                            $username = $connection->real_escape_string($username);
                            $check_exist->execute();
                            $check_exist->store_result();
                            $numRows = $check_exist->num_rows();
                            if($numRows = -1){
                                    echo "$numRows";
                                    $enter_user = $connection->prepare("INSERT INTO users (username, password) VALUES (?,?)");
                                    $enter_user->bind_param('ss', $username, $password);
                                    $enter_user->execute();
                                    $enter_user->store_result();
                                    $check_exist->close();
                                    $enter_user->close();
                                    $connection->close();
                            }
                            else{
                                    echo "This user already exists.";
                                    $check_exist->close();
                                    $connection->close();
                            }
                    }
            }
            ?>

行" if($numRows = -1){"意味着您想要编码" if($numRows == -1){",这将$numRows值设置为-1。

你可以使用" if(!$numRows){"

相关内容

  • 没有找到相关文章

最新更新