MySQL checking DB help
#1

Hello, I'm making a registration system from scratch and I have a trouble. I'm trying to check if the username already exists on database but always return that the user is not registred.

Example: I enter as TestUser but in DB, `players` table, already contains a user called TestUser (`username`), and says that the name is avaliable and I can register with this name, here is the code:

Код:
public CheckForRegister(name[], playerid)
{
    new query2[240];
    mysql_format(handle, query2, sizeof(query2), "SELECT * FROM `players` WHERE `username` = '%s'", name);
    mysql_tquery(handle, query2, "", ""); // No errors on mysql_log, the query executes right
    cache_get_data(rows, fields, handle);
    if(rows) // made a printf and rows = 0 but there is a user called same!
		{
	 		new content[256];
			format(content, sizeof(content),"{FFFFFF}Username in use.\nTell another name:");
			ShowPlayerDialog(playerid, DIALOG_PLAYER_ADD, DIALOG_STYLE_INPUT, "Register", content, "Next", "");
		}
		else
		{
       // INSERT INTO PLAYERS ETC. ALWAYS IM GETTING HERE. ROWS COUNT ARE 0 BUT IN DB I HAVE ONE USER CALLED TestUser
    }
}
SOLVED. Just changed mysql_tquery to mysql_query.
Reply
#2

Execute the query in PHPMyAdmin itself (assuming you're on windows). That usually eliminates the invalid SQL.

As for the reason;

See below code. Explained as i fixed (hopefully). If you have any issues, let me know.

pawn Код:
public CheckForRegister(name[], playerid)
{
    new query[100];
    format(query,sizeof(query),"SELECT * FROM `players` WHERE `username`='%s' LIMIT 1", name);
    return mysql_tquery(handle, query, "IsPlayerRegistered", "i", playerid);
    /*


        mysql_tquery(connectionHandle, query[], callback[] = "", format[] = "", {Float,_:...});
        mysql_pquery(connectionHandle, query[], callback[] = "", format[] = "", {Float,_:...});
   
        The above need to be called outside of the function. See IsPlayerRegistered(playerid) as an example.

        IsPlayerRegistered - callback[]
        "i"                - specificer (i, d, s for example)
        playerid           - {Float,_:...} arguments (vehicleid, playerid, targetid etc)


    */

}

forward public IsPlayerRegistered(playerid); // see the specifiers i used above ("i" and playerid, being the people's account we're checking for)
public IsPlayerRegistered(playerid)
{
    new rows, fields;
    cache_get_data(rows, fields, handle);
 
    if(rows) /* NOTE: Since you're checking if there's an account, you CAN use cache_num_rows(); if you're fussy about line count */
    {
        ShowPlayerDialog(playerid, DIALOG_PLAYER_ADD, DIALOG_STYLE_INPUT, "Register", "{FFFFFF}Username in use.\nChoose another name:", "Next", "");
    }
    else
    {
            // nothing found
    }
    return 1;
}
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)