SA-MP Forums Archive
need some help with a MySQL Connection - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+---- Forum: Help Archive (https://sampforum.blast.hk/forumdisplay.php?fid=89)
+---- Thread: need some help with a MySQL Connection (/showthread.php?tid=131988)



need some help with a MySQL Connection - TheChaoz - 06.03.2010

hi, i'm making a MySQL connection, and I get a problem, when i enter the server it crash.
it stats happening then i put this on the callback OnPlayerConnect
pawn Код:
new playername[MAX_PLAYER_NAME], query[128];
    GetPlayerName(playerid, playername, sizeof(playername));
    format(query, sizeof(query), "SELECT UserName FROM 'usuarios' WHERE 'UserName' = '%s'", playername);
  ConexionMySQL();
  mysql_real_escape_string(playername, playername);
    mysql_query(query);
    mysql_store_result();
    if(mysql_num_rows() == 1){
        PlayerInfo[playerid][Registered] = 1;
    }
    else{
        PlayerInfo[playerid][Registered] = 0;
    }
    mysql_store_result();
there are no errors/warning when i compile the .pwn

and the log sed:
Код:
[01:11:32] Incoming connection: 127.1.1.1:2092
[01:11:32] [join] The_Chaoz has joined the server (0:127.1.1.1)
[01:11:32] MySQL Error (0): Could not execute query. You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''usuarios' WHERE 'UserName' = 'The_Chaoz'' at line 1.
[01:11:32] MySQL Error (0): Could not store result. You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''usuarios' WHERE 'UserName' = 'The_Chaoz'' at line 1.
Can someone tell me what i'm doing wrong

PD: Sorry for my bad english


Re: need some help with a MySQL Connection - Miguel - 06.03.2010

pawn Код:
new playername[MAX_PLAYER_NAME], query[128];
GetPlayerName(playerid, playername, sizeof(playername));
mysql_real_escape(playername, playername);
format(query, sizeof(query), "SELECT * FROM `usuarios` WHERE UserName = '%s'", playername);
mysql_query(query);
mysql_store_result();
if(mysql_num_rows() == 1) PlayerInfo[playerid][Registered] = 1;
else PlayerInfo[playerid][Registered] = 0;
mysql_free_result();
That's how you should do it.


Re: need some help with a MySQL Connection - TheChaoz - 06.03.2010

server keep crashing.
pawn code:
pawn Код:
new playername[MAX_PLAYER_NAME], query[128];
GetPlayerName(playerid, playername, sizeof(playername));
format(query, sizeof(query), "SELECT * FROM `usuarios` WHERE UserName = '%s'", playername);
ConexionMySQL();
mysql_real_escape_string(playername, playername);
mysql_query(query);
mysql_store_result();
if(mysql_num_rows() == 1){
  PlayerInfo[playerid][Registered] = 1;
}
else{
  PlayerInfo[playerid][Registered] = 0;
}
mysql_store_result();
log:
Код:
[02:11:25] Incoming connection: 127.1.1.1:2509
[02:11:25] [join] The_Chaoz has joined the server (0:127.1.1.1)
[02:11:25] MySQL Error (0): 'mysql_store_result' called when result already stored. Please use 'mysql_free_result' first..
[02:11:25] MySQL Error (0): Could not execute query. You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''usuarios' WHERE UserName=The_Chaoz' at line 1.
any ideas?


Re: need some help with a MySQL Connection - Miguel - 06.03.2010

Quote:
Originally Posted by SAWC™
pawn Код:
new playername[MAX_PLAYER_NAME], query[128];
GetPlayerName(playerid, playername, sizeof(playername));
mysql_real_escape(playername, playername); // cleaning player's name
format(query, sizeof(query), "SELECT * FROM `usuarios` WHERE UserName = '%s'", playername); // format query
mysql_query(query); // query
mysql_store_result(); // store results
if(mysql_num_rows() == 1) PlayerInfo[playerid][Registered] = 1; // check results
else PlayerInfo[playerid][Registered] = 0;
mysql_free_result(); // clean memory
That's how you should do it.



Re: need some help with a MySQL Connection - TheChaoz - 06.03.2010

Quote:
Originally Posted by SAWC™
Quote:
Originally Posted by SAWC™
pawn Код:
new playername[MAX_PLAYER_NAME], query[128];
GetPlayerName(playerid, playername, sizeof(playername));
mysql_real_escape(playername, playername);
format(query, sizeof(query), "SELECT * FROM `usuarios` WHERE UserName = '%s'", playername);
mysql_query(query);
mysql_store_result();
if(mysql_num_rows() == 1) PlayerInfo[playerid][Registered] = 1;
else PlayerInfo[playerid][Registered] = 0;
mysql_free_result();
That's how you should do it.
Код:
error 017: undefined symbol "mysql_real_escape"
Pawn compiler 3.2.3664	 	 	Copyright © 1997-2006, ITB CompuPhase


1 Error.



Re: need some help with a MySQL Connection - Miguel - 06.03.2010

Sorry, it was mysql_real_escape_string(input, output):
pawn Код:
mysql_real_escape_string(playername, playername);



Re: need some help with a MySQL Connection - TheChaoz - 06.03.2010

the server still crash.

pawn code:
pawn Код:
new playername[MAX_PLAYER_NAME], query[128];
GetPlayerName(playerid, playername, sizeof(playername));
mysql_real_escape_string(playername, playername);
format(query, sizeof(query), "SELECT * FROM `usuarios` WHERE UserName = '%s'", playername);
mysql_query(query);
mysql_store_result();
if(mysql_num_rows() == 1) PlayerInfo[playerid][Registered] = 1;
else PlayerInfo[playerid][Registered] = 0;
mysql_free_result();
log:
Код:
[02:26:02] Incoming connection: 127.1.1.1:2616
[02:26:02] [join] The_Chaoz has joined the server (0:127.1.1.1)
[02:26:02] MySQL Error (0): Could not execute query. You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''usuarios' WHERE UserName=The_Chaoz' at line 1.



Re: need some help with a MySQL Connection - Miguel - 06.03.2010

This is how I do it:
pawn Код:
stock CheckAccount(playerid)
{
  MySQLCheck();

  new
    query[70];

  format(query, sizeof(query), "SELECT * FROM `players` WHERE Nickname = '%s'", Player[playerid][Name]);
  mysql_query(query);
  mysql_store_result();
  if(mysql_num_rows() > 0) Player[playerid][Registered] = 1;
  else Player[playerid][Registered] = 0;
  mysql_free_result();
}
pawn Код:
public OnPlayerConnect(playerid)
{
  ...
  CheckAccount(playerid);
  if(Player[playerid][Registered] == 1) BlaBlaBla;
  else BlaBlaBla2;
  return 1;
}



Re: need some help with a MySQL Connection - TheChaoz - 06.03.2010

don't know what i'm doing wrong

pawn code:
pawn Код:
public OnPlayerConnect(playerid)
{
  CheckAccount(playerid);
  //code....
  return 1;
}
pawn Код:
stock ConexionMySQL()
{
  if(mysql_ping() == 0)
  {
    return true;
  }
  else
  {
    mysql_reload();
    ConexionMySQL();
  }
  return false;
}

//----------------------------------------------------------------------------------
stock CheckAccount(playerid)
{
  ConexionMySQL();

  new query[70], playername[MAX_PLAYER_NAME];
  GetPlayerName(playerid, playername, sizeof(playername));
  format(query, sizeof(query), "SELECT * FROM `usuarios` WHERE UserName = '%s'", playername);
  mysql_query(query);
  mysql_store_result();
  if(mysql_num_rows() > 0) PlayerInfo[playerid][Registered] = 1;
  else PlayerInfo[playerid][Registered] = 0;
  mysql_free_result();
}
log:
Код:
[02:46:04] Incoming connection: 127.1.1.1:2841
[02:46:04] [join] The_Chaoz has joined the server (0:127.1.1.1)
[02:46:04] MySQL Error (0): Could not execute query. You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''usuarios' WHERE UserName=The_Chaoz' at line 1.



Re: need some help with a MySQL Connection - Miguel - 06.03.2010

Change ConexionMySQL() for:
pawn Код:
ChequearMySQL()
{
  if(mysql_ping() == -1) mysql_connect(SQL_HOST, SQL_USER,SQL_DB, SQL_PASS); // remember to define those
  return 1;
}
And by the way, what mysql plugin are you using?