I think the fetch part is right, but other parts of the code have some wrong things.
I did not test this code, but I think it should work:
static bool myCommand(ChatHandler* handler, const char* args)
{
if (!*args) // check that we have arguments
return false;
uint32 id = uint32(atoll((char*)args)); // convert arguments to long long int and from there to uint32
if (!id) // check that id is not 0 (was a number and not 0)
return false;
QueryResult result = WorldDatabase.PQuery("SELECT COUNT(guid) FROM creature WHERE id = %u;", id);
if(!result) // check that we have a result
{
handler->SendGlobalGMSysMessage("no result");
return true;
}
uint32 count = result->Fetch()[0].GetUInt32();
if(!count) // check that count is not 0
handler->SendGlobalGMSysMessage("no result but request sent");
std::ostringstream oss;
oss << "Result: " << count; // insert count (number) to a string IE: Result: 23454
handler->SendGlobalGMSysMessage(oss.str().c_str()); // convert the stringstream to a string and a const char* that fits the argument needed
return true; // notice that return false gives error while return true shows no msg to player (success)
}
Thx for your answer nwo the command give a result , but the fetch don’t work , i did the SQL request with SQLyog , it return 51 , and with the command in game , it said “no result but request sent Result:0”
The problem donn’t come of my request , i will try to change the row of fetch or do a sql request without variable (maybe the args is mofified during the conversion?)
Thx now the counter work well , but i still have problems with fetch()
Code:
It return the correct number of guid but not the guid ^^’
QueryResult result = WorldDatabase.PQuery("SELECT guid FROM creature WHERE id = %u;", id);
if(!result) // check that we have a result
{
handler->SendGlobalGMSysMessage("no result");
return true;
}
uint32 count = result->GetRowCount();
if(!count) // check that count is not 0
handler->SendGlobalGMSysMessage("no result but request sent");
for(uint32 i=0;i<=count;i++)
{
handler->SendGlobalGMSysMessage("Entering in the for");
uint32 guid = result->Fetch()[(int)i].GetUInt32();
std::ostringstream msg_guid;
msg_guid << "GUID n°: " << guid;
handler->SendGlobalGMSysMessage(msg_guid.str().c_str());
}
std::ostringstream oss;
oss << "Result: " << count;
handler->SendGlobalGMSysMessage(oss.str().c_str());
return true;
Btw. I think SendGlobalGMSysMessage shows to all GMs ingame. Use PSendSysMessage if this isn’t intended.
Your code doesnt work cause the number after fetch is the column ID. It starts from 0. Example:
// 0 1 2 3 4
SELECT entry, name, subname, damage, armor FROM creature_template WHERE entry = 123;
QueryResult result = WorldDatabase.PQuery("SELECT guid FROM creature WHERE id = %u;", id);
if(!result) // check that we have a result
{
handler->SendGlobalGMSysMessage("no result");
return true;
}
uint32 count = result->GetRowCount(); // get row count
if(!count) // check that count is not 0
handler->SendGlobalGMSysMessage("no result but request sent");
handler->SendGlobalGMSysMessage("Entering in the do loop");
do // do-while loop
{
uint32 guid = result->Fetch()[0].GetUInt32(); // 0 since guid is the first column in the returned value list in the SQL
std::ostringstream msg_guid;
msg_guid << "GUID n°: " << guid;
handler->SendGlobalGMSysMessage(msg_guid.str().c_str());
// NextRow is called in the end to move onto the next SQL row!
} while (result->NextRow()); // stops when there is no next row. (NextRow() returns false)
std::ostringstream oss;
oss << "Result: " << count;
handler->SendGlobalGMSysMessage(oss.str().c_str());
return true;
There’s no need to do (int)i since i is uint32, and the array index only supports unsigned numbers.
is not a row indicator, yet it indicates the column of your resultset. with your query you will only have 1 column, so any value of i over 0 is invalid.
Ok! i didn’t know about NextRow() , i tried de code of rochet2 and machiavelli and both doesn’t work :-/ , the command do the right loop (confirmed with a message at each loop) but it don’t show any guid .
Yeah, lol, because it’s not written to output anything. It was an example on how to use Fetch() so you could put it in your script. I´m not going to rewrite your entire script for you.