Search code examples
c#mysql.netmysql.data

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


I'm trying to check if the user's HWID already exists in my 'users' table in mysql database when trying to login. And if it does exist, make another check. However I'm getting error called "You have an error in your SQL syntax; check the manual corresponds to your MySQL server version..." and so on. There doesn't seem to be any errors in the syntax. I think the issue would be in int part. I'm not sure so I need your help! SQL Syntax Error Screenshot on imgur

I've tried to google this and search on stackoverflow (here). I haven't found answer to my "actual" issue. I tried to use boolean instead of 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)

I want it to check if the hwid is in database and continue to next check.


Solution

  • You are missing (*) in your sql.

    It should be some thing like this:

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

    This is exactly what the debugger is telling you, something is wrong with SQL syntax.

    Another thing I have seen is, to check the count result you need to change your logic a bit.

    from this:

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

    to this:

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