我如何使用 int 检查 MySQL 数据库中是否存在某些内容,或者是否有其他选择?

How can I check if something exist in MySQL database with int, or is there any alternatives?

我试图在尝试登录时检查用户的 HWID 是否已经存在于我的 'users' table 和 mysql 数据库中。如果确实存在,请进行另一次检查。但是,我收到名为 "You have an error in your SQL syntax; check the manual corresponds to your MySQL server version..." 的错误,依此类推。语法上似乎没有任何错误。我认为这个问题将在 int 部分。我不确定所以我需要你的帮助!

我已经尝试 google 这个并在 Whosebug 上搜索(这里)。我还没有找到 "actual" 问题的答案。我尝试使用 boolean 而不是 int。

MySqlCommand hwid = con.CreateCommand();
                hwid.CommandType = CommandType.Text;
                hwid.CommandText = "SELECT COUNT * FROM users where hwid='"+leet+"' AND username='"+textBox1.Text+"'";
                hwid.Parameters.AddWithValue("hwid", leet);
                hwid.Parameters.AddWithValue("username", textBox1.Text);
                int hwidCheck1 = hwid.ExecuteNonQuery();
                if ((int)hwidCheck1 > 0)

我想让它检查 hwid 是否在数据库中并继续下一个检查。

您的 sql 中缺少 (*)。

应该是这样的:

SELECT COUNT(*) FROM users where hwid='whatever' AND username='someuser'

这正是调试器告诉您的内容,SQL 语法有问题。

我看到的另一件事是,要检查计数结果,您需要稍微更改一下逻辑。

来自这个:

int hwidCheck1 = hwid.ExecuteNonQuery();
if ((int) hwidCheck1 > 0)

对此:

int hwidCheck1 = int.Parse(hwid.ExecuteScalar().ToString());
if (hwidCheck1 > 0)