Mysql - connection already exists
#1

I got this warning in the mysql log file whenever I try to login into my account (ingame),
Код:
[13:58:50] [WARNING] CMySQLHandle::Create - connection already exists
+the registration works however there is something wrong with the login system, I can't login.
Below you can check the codes I used from this tutorial

First part:
PHP код:
//MYSQL*********************************//
#define SQL_HOST "localhost"
#define SQL_USER "root"
#define SQL_PASS ""
#define SQL_DB "test"
static mysql;
//MYSQL*********************************//
//ACCOUNT INFO VARIABLES ***************//
enum PDATA 
{
    
Password[129], //We will load player's password into this varible from database
    
userid,
}
new 
pInfo[MAX_PLAYERS][PDATA];
#define dlogin        0
#define dregister    1
forward OnAccountLoad(playerid);
forward OnAccountRegister(playerid);
native WP_Hash(buffer[], len, const str[]); 
//***************************************// 
Second part: Functions part
PHP код:
forward ConnectMySQL();
public 
ConnectMySQL()
{
    if(
mysql_errno(mysql) == 0)
    {
        
mysql_log(LOG_ERROR LOG_WARNINGLOG_TYPE_TEXT);
        
printf("[MYSQL]: Connected to `%s`.",SQL_DB);
    }
    else 
printf("[MYSQL]: Failed to connect to `%s`.",SQL_DB);
    return 
1;
}
forward OnAccountCheck(playerid);
public 
OnAccountCheck(playerid)
{
    new 
rowsfields
    
cache_get_data(rowsfieldsmysql);
    if(
rows
    {
        
cache_get_field_content(0"password"pInfo[playerid][Password], mysql129);
        
pInfo[playerid][userid] = cache_get_field_content_int(0"userid");
        
ShowPlayerDialog(playeriddloginDIALOG_STYLE_INPUT"Login""login bitch""Login""Leave");
    }
    else
    {
        
ShowPlayerDialog(playeriddregisterDIALOG_STYLE_INPUT"Registration""You must be new fool""Register""Quit");
    }
    return 
1;
}
public 
OnAccountLoad(playerid)
{
    
SendClientMessage(playerid, -1"Successfully logged in");
    return 
1;
}
public 
OnAccountRegister(playerid)
{
    
pInfo[playerid][userid] = cache_insert_id(); 
    
printf("New account registered. ID: %d"pInfo[playerid][userid]); 
    return 
1;

Dialogs part:
PHP код:
public OnDialogResponse(playeriddialogidresponselistiteminputtext[])
{
    switch(
dialogid)
    {
        case 
dlogin:
        {
            if(!
responseKick(playerid);
            new 
query[100], hpass[129];
            
WP_Hash(hpass129inputtext);
            if(!
strcmp(hpasspInfo[playerid][Password]))
            {           
                new 
Name[28];
                
GetPlayerName(playeridName28);
                
mysql_format(mysqlquerysizeof(query), "SELECT * FROM `users` WHERE `name` = '%e' LIMIT 1"Name);
                
mysql_tquery(mysqlquery"OnAccountLoad""i"playerid);
            }
            else 
ShowPlayerDialog(playeriddloginDIALOG_STYLE_INPUT"Login""login bitch\nWrong password!""Login""Quit");
        }
        case 
dregister:
        {
            if(!
response) return Kick(playerid);
            if(
strlen(inputtext) < 6) return ShowPlayerDialog(playeriddregisterDIALOG_STYLE_INPUT"Register""You must be new here fool .\nYour password must be at least 6 characters long!""Register""Quit");
            new 
query[300], Name[28];
            
GetPlayerName(playeridName28);
            
WP_Hash(pInfo[playerid][Password], 129inputtext); //hashing inputtext
            
mysql_format(mysqlquerysizeof(query), "INSERT INTO `users` (`name`, `password`) VALUES ('%e', '%s')"NamepInfo[playerid][Password]);
            
mysql_tquery(mysqlquery"OnAccountRegister""i"playerid);
        }
    }
    return 
1;

OnGameModeInit part:
PHP код:
mysql mysql_connect(SQL_HOSTSQL_USERSQL_DBSQL_PASS);
    
ConnectMySQL(); 
And finally this on player class selection:
PHP код:
new query[128], name[50];
        
GetPlayerName(playeridname50);
        
mysql_format(mysqlquerysizeof(query),"SELECT `password`, `userid` FROM `users` WHERE `name` = '%e' LIMIT 1"name);
        
mysql_tquery(mysqlquery"OnAccountCheck""i"playerid); 
Im totally new to MySQL, appreciate your help.
Reply
#2

Code updated, but im still wondering why this doesn't work
Reply
#3

Where is actually your mysql_connect line? It should be like this:
pawn Код:
ConnectMySQL()
{
    mysql_log(LOG_ERROR | LOG_WARNING, LOG_TYPE_TEXT);
    mysql = mysql_connect(...);
 
    if (!mysql_errno(mysql)) printf("[MYSQL]: Connected to `%s`.",SQL_DB);
    else printf("[MYSQL]: Failed to connect to `%s`.",SQL_DB);
}
and in OnGameModeInit:
pawn Код:
ConnectMySQL();
Reply
#4

Quote:
Originally Posted by Konstantinos
Посмотреть сообщение
Where is actually your mysql_connect line? It should be like this:
pawn Код:
ConnectMySQL()
{
    mysql_log(LOG_ERROR | LOG_WARNING, LOG_TYPE_TEXT);
    mysql = mysql_connect(...);
 
    if (!mysql_errno(mysql)) printf("[MYSQL]: Connected to `%s`.",SQL_DB);
    else printf("[MYSQL]: Failed to connect to `%s`.",SQL_DB);
}
and in OnGameModeInit:
pawn Код:
ConnectMySQL();
Still the same; registration works but i can't login
Reply
#5

Let's view the things again. You got:
pawn Код:
[WARNING] CMySQLHandle::Create - connection already exists
which happens if you call mysql_connect more than once in the same script.

The code seems pretty much fine (don't forget to use "return" if there is no response) so debugging:
pawn Код:
printf("hpass: \"%s\" & pInfo[playerid][Password]: \"%s\"", hpass, pInfo[playerid][Password]);
if(!strcmp(hpass, pInfo[playerid][Password]))
What does it print?
Reply
#6

Thanks, i got it, its all about the string length

print result:
Код:
[17:31:14] hpass: "CFD6DB2D5800215F84C2455945C233C6F8404554960771A0D444A9905EDCAA3AEFFA0C32B1BA34BC4156580123F540A412D7822CB07ABD164607149850FCC1E6" & pInfo[playerid][Password]: "CFD6DB2D5800215F84C2455945C233C6F8404554960771A0D4"
EDIT: just to make things more clear, when i have created the table i have set the max characters limit of "password' to 50, that's why it was not working.
Reply


Forum Jump:


Users browsing this thread: 3 Guest(s)