尝试使用PHP检查MySQL数据库中是否已存在用户名

时间:2022-04-08 04:26:48

I've looked at the many other posts that were similar to my issue and implemented their solutions (as far as I can tell) as exactly as I could. However, every time I execute this script, the code in the else block is executed (even when the username inputted is one that is already present).

我已经查看过与我的问题类似的许多其他帖子,并尽可能地实现了他们的解决方案(据我所知)。但是,每次执行此脚本时,都会执行else块中的代码(即使输入的用户名是已存在的用户名)。

The table name is 'Users' and the column that I am trying to search is 'username'. The input from my form was read into $username and I verified that it was read in properly using echo. $con contains the connection to the server.

表名是“用户”,我要搜索的列是“用户名”。我的表单中的输入被读入$ username,我验证它是使用echo正确读取的。 $ con包含与服务器的连接。

At some point I also put in echo $query (nothing was printed) and echo mysql_num_rows($query) (nothing was printed).

在某些时候我也放入echo $ query(没有打印)和echo mysql_num_rows($ query)(没有打印)。

Here's the relevant segment of the code. Would really appreciate some tips.

这是代码的相关部分。真的很感激一些提示。

$query = mysql_query("SELECT username FROM Users WHERE username=$username", $con);

  if (mysql_num_rows($query) != 0)
  {
      echo "Username already exists";
  }

  else
  {
    ...
  }

EDIT: Apparently I was supposed to be using mysqli for my server and the way I checked the num_rows for that was by doing $query->num_rows since it was a property of the object. Thanks for all the help!

编辑:显然我应该使用mysqli为我的服务器和我检查num_rows的方式是通过执行$ query-> num_rows,因为它是对象的属性。感谢您的帮助!

9 个解决方案

#1


9  

change your query to like.

将您的查询更改为喜欢。

$query = mysql_query("SELECT username FROM Users WHERE username='".$username."'");

#2


4  

Try this:

尝试这个:

$query = mysql_query("SELECT username FROM Users WHERE username='$username' ")

Don't add $con to mysql_query() function

不要将$ con添加到mysql_query()函数中

#3


3  

I think there is something wrong in the code. You can remove the $con, that's not necessary and

我认为代码中有问题。您可以删除$ con,这不是必需的

The problem is with the php variable inside mysql query. You have to put a single quote around it. This code should work just fine

问题是mysql查询中的php变量。你必须在它周围加一个引号。这段代码应该可以正常工作

$query = mysql_query("SELECT username FROM Users WHERE username='$username'");

  if (mysql_num_rows($query) != 0)
  {
      echo "Username already exists";
  }

  else
  {
    ...
  }

#4


0  

TRY THIS ONE

尝试这一个

 mysql_connect('localhost','dbuser','dbpass');

$query = "SELECT username FROM Users WHERE username='".$username."'";
mysql_select_db('dbname');

    $result=mysql_query($query);

   if (mysql_num_rows($query) != 0)
   {
     echo "Username already exists";
    }

    else
   {
     ...
    }

#5


0  

Everything is fine, just one mistake is there. Change this:

一切都很好,只有一个错误。改变这个:

$query = mysql_query("SELECT username FROM Users WHERE username=$username", $con);
$query = mysql_query("SELECT Count(*) FROM Users WHERE username=$username, $con");

if (mysql_num_rows($query) != 0)
{
    echo "Username already exists";
}
else
{
  ...
}

SELECT * will not work, use with SELECT COUNT(*).

SELECT *不起作用,使用SELECT COUNT(*)。

#6


0  

$firstname = $_POST["firstname"];
$lastname = $_POST["lastname"];
$email = $_POST["email"];
$pass = $_POST["password"];

$check_email = mysqli_query($conn, "SELECT Email FROM crud where Email = '$email' ");
if(mysqli_num_rows($check_email) > 0){
    echo('Email Already exists');
}
else{
    if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $result = mysqli_query($conn, "INSERT INTO crud (Firstname, Lastname, Email, Password) VALUES ('$firstname', '$lastname', '$email', '$pass')");
}
    echo('Record Entered Successfully');
}

#7


0  

Here's one that i wrote:

这是我写的一个:

$error = false;
$sql= "SELECT username FROM users WHERE username = '$username'";
$checkSQL = mysqli_query($db, $checkSQL);

if(mysqli_num_rows($checkSQL) != 0) {
   $error = true;
   echo '<span class="error">Username taken.</span>';
}

Works like a charm!

奇迹般有效!

#8


-1  

Change this

改变这个

$query = mysql_query("SELECT username FROM Users WHERE username=$username", $con);

To

$query = mysql_query("SELECT username FROM Users WHERE username='$username'");

#9


-1  

PHP 7 improved query.........

PHP 7改进了查询.........

$sql = mysqli_query($conn, "SELECT * from users WHERE user_uid = '$uid'"); if (mysqli_num_rows($sql) > 0) { echo 'Username taken.'; }

$ sql = mysqli_query($ conn,“SELECT * from users WHERE user_uid ='$ uid'”); if(mysqli_num_rows($ sql)> 0){echo'Username taken。'; }

#1


9  

change your query to like.

将您的查询更改为喜欢。

$query = mysql_query("SELECT username FROM Users WHERE username='".$username."'");

#2


4  

Try this:

尝试这个:

$query = mysql_query("SELECT username FROM Users WHERE username='$username' ")

Don't add $con to mysql_query() function

不要将$ con添加到mysql_query()函数中

#3


3  

I think there is something wrong in the code. You can remove the $con, that's not necessary and

我认为代码中有问题。您可以删除$ con,这不是必需的

The problem is with the php variable inside mysql query. You have to put a single quote around it. This code should work just fine

问题是mysql查询中的php变量。你必须在它周围加一个引号。这段代码应该可以正常工作

$query = mysql_query("SELECT username FROM Users WHERE username='$username'");

  if (mysql_num_rows($query) != 0)
  {
      echo "Username already exists";
  }

  else
  {
    ...
  }

#4


0  

TRY THIS ONE

尝试这一个

 mysql_connect('localhost','dbuser','dbpass');

$query = "SELECT username FROM Users WHERE username='".$username."'";
mysql_select_db('dbname');

    $result=mysql_query($query);

   if (mysql_num_rows($query) != 0)
   {
     echo "Username already exists";
    }

    else
   {
     ...
    }

#5


0  

Everything is fine, just one mistake is there. Change this:

一切都很好,只有一个错误。改变这个:

$query = mysql_query("SELECT username FROM Users WHERE username=$username", $con);
$query = mysql_query("SELECT Count(*) FROM Users WHERE username=$username, $con");

if (mysql_num_rows($query) != 0)
{
    echo "Username already exists";
}
else
{
  ...
}

SELECT * will not work, use with SELECT COUNT(*).

SELECT *不起作用,使用SELECT COUNT(*)。

#6


0  

$firstname = $_POST["firstname"];
$lastname = $_POST["lastname"];
$email = $_POST["email"];
$pass = $_POST["password"];

$check_email = mysqli_query($conn, "SELECT Email FROM crud where Email = '$email' ");
if(mysqli_num_rows($check_email) > 0){
    echo('Email Already exists');
}
else{
    if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $result = mysqli_query($conn, "INSERT INTO crud (Firstname, Lastname, Email, Password) VALUES ('$firstname', '$lastname', '$email', '$pass')");
}
    echo('Record Entered Successfully');
}

#7


0  

Here's one that i wrote:

这是我写的一个:

$error = false;
$sql= "SELECT username FROM users WHERE username = '$username'";
$checkSQL = mysqli_query($db, $checkSQL);

if(mysqli_num_rows($checkSQL) != 0) {
   $error = true;
   echo '<span class="error">Username taken.</span>';
}

Works like a charm!

奇迹般有效!

#8


-1  

Change this

改变这个

$query = mysql_query("SELECT username FROM Users WHERE username=$username", $con);

To

$query = mysql_query("SELECT username FROM Users WHERE username='$username'");

#9


-1  

PHP 7 improved query.........

PHP 7改进了查询.........

$sql = mysqli_query($conn, "SELECT * from users WHERE user_uid = '$uid'"); if (mysqli_num_rows($sql) > 0) { echo 'Username taken.'; }

$ sql = mysqli_query($ conn,“SELECT * from users WHERE user_uid ='$ uid'”); if(mysqli_num_rows($ sql)> 0){echo'Username taken。'; }