PHP检查用户名是否存在

时间:2015-06-02 03:28:06

标签: php mysql sql select insert

好的,所以当我登录页面时,我收到此错误

  

警告:mysql_num_rows():提供的参数不是有效的MySQL   结果资源在/home/a1982502/public_html/test3/register.php上   第55行

这是PHP

<?php 
if(isset($_POST['submit'])) {
    $servername = "mysql10.000webhost.com";
    $username = "a1982502_test";
    $password = "";
    $dbname = "a1982502_test";

    $password1 = $_POST['password1'];
    $password2 = $_POST['password2'];
    $username1 = $_POST['username1'];
    $email = $_POST['email'];
    if(!empty($username) && !empty($password1) && !empty($password2) && !empty($email)) {
        if($password1 == $password2) {
            $conn = new mysqli($servername, $username, $password, $dbname);
            //send data
            $sql = "SELECT COUNT(*) FROM users WHERE user = '$username1'";
            if (mysql_num_rows($sql) > 0) {
                echo "Username already exists";
                mysqli_close($conn);
                exit();
            } else {
                $password1 = md5($password1);
                $sql = "INSERT INTO users (id, user, email, pass) VALUES (NULL, '$username1', '$email', '$password1')";
                if (mysqli_query($conn, $sql)) {
                    echo "Registered!";
                }
                mysqli_close($conn);
                exit();
            }           
        } else {
            //error
            Echo "Password is wrong";

            exit();
        }
    }
}
?>

3 个答案:

答案 0 :(得分:1)

您没有运行查询。初始化$ SQL后执行此操作,

$result = mysqli_query($conn, $sql);

然后你可以调用mysqli_num_rows($ result)。

答案 1 :(得分:-1)

您需要更改查询以选择* from而不是select count(*)以使mysql_num_rows()起作用。

我看到你没有在任何地方调用mysql_query()。为了使查询实际运行,您需要传递此函数。你也可以使用mysqli我有一段时间没有使用经典的PHP所以这是一个未经测试的应该工作的样本

<?php 
if(isset($_POST['submit']))
{
    $servername = "mysql10.000webhost.com";
    $username = "a1982502_test";
    $password = "";
    $dbname = "a1982502_test";

    $password1 = $_POST['password1'];
    $password2 = $_POST['password2'];
    $username1 = $_POST['username1'];
    $email = $_POST['email'];

    if(!empty($username) && !empty($password1) && !empty($password2) && !empty($email))
    {
        if($password1 == $password2)
        {
            $conn = new mysqli($servername, $username, $password, $dbname);
            //send data
            $sql = mysql_query("SELECT * FROM users WHERE user = '$username1'");

            if (mysql_num_rows($sql) > 0)
            {
                echo "Username already exists";
                mysqli_close($conn);
                exit();
            } 
            else
            {
                $password1 = md5($password1);
                $sql = mysql_query("INSERT INTO users (id, user, email, pass) VALUES (NULL, '$username1', '$email', '$password1')");

                mysqli_close($conn);
                exit();
            }           
        } 
        else
        {
            //error
            Echo "Password is wrong";
            exit();
        }
    }
}
?>

答案 2 :(得分:-1)

进行如下修正

$sql = "SELECT * FROM users WHERE user = '$username1'";   
if ($result=mysqli_query($conn,$sql))
{
  // Check no of rows
  if(mysqli_num_rows($result))
  {
            echo "Username already exists";
            mysqli_close($conn);
            exit();
  }else 
  {
  // Register
            $password1 = md5($password1);
            $sql = "INSERT INTO users (id, user, email, pass) VALUES (NULL, '$username1', '$email', '$password1')";
            if (mysqli_query($conn, $sql))
            echo "Registered!";
            mysqli_close($conn);
            exit();
  }  

}else
{
    // Query failed
    echo("Error description: " . mysqli_error($conn));
}