IsUserNameAvailable doesn't work
#1

This function doesn't work, and I don't know why. The mysql connection information, and table information is not wrong.

Anybody?


The Function
Код:
stock IsUserNameAvailable(playername[])
{
	format(query,sizeof(query),"select * FROM `users` WHERE `username`='%s' limit 0,1;",playername);
	mysql_query(query);
	mysql_store_result();
	playername[0]=mysql_fetch_int();
	mysql_free_result();
	if(playername[0]==1)return 1;
	return 0;
}
On OnPlayerConnect
Код:
	if(!IsUserNameAvailable(PlayerName(playerid)))
	{
	    //if(OnPlayerAutoLogin(playerid)){SetPVarInt(playerid, "playerstatus",2); return SpawnPlayer(playerid);}
		ShowPlayerDialog(playerid,DIALOGID+1,DIALOG_STYLE_MSGBOX,ServerInfo[sName],"The system found an account by your nickname. \nYou have to login to continue.","Login","Quit");
	}else{
		ShowPlayerDialog(playerid,DIALOGID+2,DIALOG_STYLE_MSGBOX,ServerInfo[sName],"The system didn't found an account by your nickname. You can play with or without an account. \nIf you don't register, you stats won't be restored. If you register your stats will be stored. \nPlease make your choice.","Register","Don't Reg.");
	}
Reply
#2

Thanks for your answer!

I'm new with MySQL, so it's possible that I make some stupid mistakes.

It's still not working, sometimes it shows that it does exist, and sometimes it shows that it doesn't exist.

Fuction
Код:
stock IsUserNameAvailable(playername[])
{
	format(query,sizeof(query),"SELECT COUNT(username) FROM `users` WHERE `username`='%s' limit 0,1;",playername);
	mysql_query(query);
	mysql_store_result();
	playername[0]=mysql_fetch_int();
	mysql_free_result();
	if(playername[0]==0)return 1;
	return 0;
}
Errors
Код:
[23:41:46] CMySQLHandler::Query(SELECT COUNT(username) FROM `users` WHERE `username`='BritishBoy' limit 0,1;) - An error has occured. (Error ID: 2013, Lost connection to MySQL server during query)

[23:41:46] >> mysql_store_result( Connection handle: 1 )

[23:41:46] CMySQLHandler::StoreResult() - No data to store.

[23:41:46] >> mysql_fetch_int( Connection handle: 1 )

[23:41:46] CMySQLHandler::FetchRow() - You cannot call this function now. (Reason: Empty Result)

[23:41:46] >> mysql_free_result( Connection handle: 1 )

[23:41:46] CMySQLHandler::FreeResult() - The result is already empty.
Reply
#3

Why not just "SELECT NULL" and than check with mysql_num_rows()?
Reply
#4

Try like this, but Im not expert in SQL yet

pawn Код:
stock IsUserNameAvailable(playername[])
{
    new esc[24],query[256];
    mysql_real_escape_string(playername,esc);
    format(query,sizeof(query),"SELECT COUNT(username) FROM `users` WHERE `username`='%s'",esc);
    mysql_query(query);
    mysql_store_result();
    if(mysql_fetch_int() > 0) { return 0; }
    else { return 1; }
}
Reply
#5

Thank!, but it doesn't fix the bug

Код:
stock IsUserNameAvailable(playername[])
{
    new esc[24],query2[256];
    mysql_real_escape_string(playername,esc);
    format(query2,sizeof(query2),"SELECT COUNT(username) FROM `users` WHERE `username`='%s'",esc);
    mysql_query(query2);
    mysql_store_result();
    if(mysql_fetch_int() > 0) { return 0; }
    else { return 1; }
}
Код:
[00:20:01] CMySQLHandler::Query(SELECT COUNT(username) FROM `users` WHERE `username`='TestName') - An error has occured. (Error ID: 2013, Lost connection to MySQL server during query)

[00:20:01] >> mysql_store_result( Connection handle: 1 )

[00:20:01] CMySQLHandler::StoreResult() - No data to store.

[00:20:01] >> mysql_fetch_int( Connection handle: 1 )

[00:20:01] CMySQLHandler::FetchRow() - You cannot call this function now. (Reason: Empty Result)
Reply
#6

Try this
pawn Код:
stock IsUserNameAvailable(playername[])
{
    new esc[24],query2[256];
    mysql_real_escape_string(playername,esc);
    format(query2,sizeof(query2),"SELECT * FROM `users` WHERE `username`='%s'",esc);
    mysql_query(query2);
    mysql_store_result();
    if(mysql_num_rows()){
       mysql_free_result();
       return 1;
    } else {
       mysql_free_result();
       return 0;
    }

}
Reply
#7

Quote:
Originally Posted by ******
Посмотреть сообщение
The fact that you're loosing connection during the query is the most major issue! Have you correctly opened a connection and where is the MySQL server relative to the SA:MP server?
This was the problem. I've try WAMP and now it works fine.

Thanks all!!!
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)