Jump to content
  • 0

Get IP by query c++


avertusss

Question

Hi. I want to make function which check ip of inserted player name. I have

ACMD(do_checkplayer)
{
	char arg1[256];
	one_argument(argument, arg1, sizeof(arg1));
	
	if (!*arg1 || strlen(arg1) < 3)
	{
		ch->ChatPacket(CHAT_TYPE_INFO, "Enter player name");
		return;
	}
	
	char ip[256];
	
	std::unique_ptr<SQLMsg> pkMsg2(DBManager::instance().DirectQuery("SELECT IP from player where name = '%s'", arg1));
	SQLResult * pRes2 = pkMsg2->Get();

	if (pRes2->uiNumRows > 0)
	{
		MYSQL_ROW row;
		while ((row = mysql_fetch_row(pRes2->pSQLResult)) != NULL)
		{
			sql_IP = row[0];
		}
		ip = sql_IP;
	}
	
	ch->ChatPacket(CHAT_TYPE_INFO, "%s", ip);
}

I don't know how to exactly return that ip into variable and use it everywhere in that function. Can someone help me? 

Edited by avertusss
Link to comment
Share on other sites

  • Answers 5
  • Created
  • Last Reply

Top Posters For This Question

Popular Days

Top Posters For This Question

5 answers to this question

Recommended Posts

  • 0
  • Premium
30 minutes ago, avertusss said:

Hi. I want to make function which check ip of inserted player name. I have

ACMD(do_checkplayer)
{
	char arg1[256];
	one_argument(argument, arg1, sizeof(arg1));
	
	if (!*arg1 || strlen(arg1) < 3)
	{
		ch->ChatPacket(CHAT_TYPE_INFO, "Enter player name");
		return;
	}
	
	char ip[256];
	
	std::unique_ptr<SQLMsg> pkMsg2(DBManager::instance().DirectQuery("SELECT IP from player where name = '%s'", arg1));
	SQLResult * pRes2 = pkMsg2->Get();

	if (pRes2->uiNumRows > 0)
	{
		MYSQL_ROW row;
		while ((row = mysql_fetch_row(pRes2->pSQLResult)) != NULL)
		{
			sql_IP = row[0];
		}
		ip = sql_IP;
	}
	
	ch->ChatPacket(CHAT_TYPE_INFO, "%s", ip);
}

I don't know how to exactly return that ip into variable and use it everywhere in that function. Can someone help me? 

char szQuery[50]; // << the length of your string
snprintf(szQuery, sizeof(szQuery), "SELECT IP FROM player.player WHERE name = '%s'", arg1);
std::unique_ptr<SQLMsg> msg(DBManager::instance().DirectQuery(szQuery));

MYSQL_RES* Result = msg->Get()->pSQLResult;
MYSQL_ROW row = mysql_fetch_row(Result);
std::string IP = row[0];

if you select a string you can just create one if you use an integer you have to use this function :
 

str_to_number(IP, row[0]);

 

Link to comment
Share on other sites

  • 0
Posted (edited)
26 minutes ago, xTryhard said:
char szQuery[50]; // << the length of your string
snprintf(szQuery, sizeof(szQuery), "SELECT IP FROM player.player WHERE name = '%s'", arg1);
std::unique_ptr<SQLMsg> msg(DBManager::instance().DirectQuery(szQuery));

MYSQL_RES* Result = msg->Get()->pSQLResult;
MYSQL_ROW row = mysql_fetch_row(Result);
std::string IP = row[0];

if you select a string you can just create one if you use an integer you have to use this function :
 

str_to_number(IP, row[0]);

 

But if i use str_to_number i can''t compare it with other select bcs number doens't have . so ip won't be 192.168.0.1 but 19216801 or idk how.

 

I want use that checked ip in other mysql query like this

std::unique_ptr<SQLMsg> pkMsg(DBManager::instance().DirectQuery("SELECT player.name as '0' FROM player WHERE player.last_play > NOW()-600 and player.IP='whatshouldiwritethere????"));

 

Edited by avertusss
Link to comment
Share on other sites

  • 0
  • Premium
7 minutes ago, avertusss said:

But if i use str_to_number i can''t compare it with other select bcs number doens't have . so ip won't be 192.168.0.1 but 19216801 or idk how.

 

I want use that checked ip in other mysql query like this

std::unique_ptr<SQLMsg> pkMsg(DBManager::instance().DirectQuery("SELECT player.name as '0' FROM player WHERE player.last_play > NOW()-600 and player.IP='whatshouldiwritethere????"));

 

what do you want to do exactly?

Link to comment
Share on other sites

  • 0
Posted (edited)
24 minutes ago, xTryhard said:

what do you want to do exactly?

I want to check other active characters owned by checked player(that's why i need ip address for that). I have something like this

ACMD(do_checkplayer)
{
	char arg1[256];
	one_argument(argument, arg1, sizeof(arg1));
	
	if (!*arg1 || strlen(arg1) < 3)
	{
		ch->ChatPacket(CHAT_TYPE_INFO, "Enter player name");
		return;
	}
	
	char playerip[256];
	
	std::unique_ptr<SQLMsg> pkMsg2(DBManager::instance().DirectQuery("SELECT IP from player where name = '%s'", arg1));
	SQLResult * pRes2 = pkMsg2->Get();

	if (pRes2->uiNumRows > 0)
	{
		MYSQL_ROW row;
		while ((row = mysql_fetch_row(pRes2->pSQLResult)) != NULL)
		{
			sql_IP = row[0];
		}
		playerip = sql_IP;
	}
	
	ch->ChatPacket(CHAT_TYPE_INFO, "%s", playerip);
	
	std::string name;
	std::string buffer;
	{
		std::unique_ptr<SQLMsg> pkMsg(DBManager::instance().DirectQuery("SELECT player.name as '0' FROM player WHERE player.last_play > NOW()-600 and player.IP='%s'", playerip));
		SQLResult * pRes = pkMsg->Get();
		if (pRes->uiNumRows>0)
		{
			MYSQL_ROW row;
			int count = 0;
			ch->ChatPacket(CHAT_TYPE_INFO, "Character list:");
			while ((row = mysql_fetch_row(pRes->pSQLResult)) != NULL)
			{
				buffer = row[0];
				count += buffer.length() + 3;
				if(count > 110)
				{
					ch->ChatPacket(CHAT_TYPE_INFO, "%s", name.c_str());
					name="";
					count = 0;
				}
				name += " | ";
				name += "|cffffc700|Hwhisper:";
				name += buffer;
				name += "|h";
				name += buffer;
				name += "|h|r";
			}
			ch->ChatPacket(CHAT_TYPE_INFO, "%s", name.c_str());
		}
		else
			ch->ChatPacket(CHAT_TYPE_INFO, "No available characters in 10 min.");
	}
}

 

Edited by avertusss
Link to comment
Share on other sites

Guest
This topic is now closed to further replies.


×
×
  • Create New...

Important Information

Terms of Use / Privacy Policy / Guidelines / We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.