Im working on a small code and am trying to limit the size of the mysql databse string its pulling.
It can only be 119 characters, or less if its more i would like to do nothing, but if its meets the requirements it runs the script.
string_t message ="Is requesting some one to respond.";<_______________TEMP SHOULD BE THE POSTERS MESSAGE
string_t username = "Guest";<_______________TEMP SHOULD BE THE POSTERS NAME
// char will not be logged in so get the id manually
const int8* Query = "SELECT message_id, username, message FROM phpbb_chat WHERE count = '0';";
int32 ret = Sql_Query(SqlHandle,Query);
if (ret != SQL_ERROR && Sql_NumRows(SqlHandle) != 0 && Sql_NextRow(SqlHandle) == SQL_SUCCESS)
message_id = (int32)Sql_GetIntData(SqlHandle,0);
username = Sql_GetData(SqlHandle,0);
message = Sql_GetData(SqlHandle,1);
//So here is where I'm heaving the problem
if(message is less then 119 characters run script )<<_______________________________________________THIS IS THE CODE LINE IM TRYING TO LEARN
sprintf(buf,"[Web Chat] %s %s",username.c_str(), message.c_str());
for (uint16 zone = 0; zone < 256; ++zone)
new CChatMessageStringPacket(PChar, MESSAGE_STRING_SAY , ("%s",buf)));
ShowNotice(CL_RED"TRACER:COMMAND: CRecastContainer::Check MessageID %u Username %s Message %s \n" CL_RESET,message_id,username.c_str(),message.c_str());
ShowNotice(CL_RED"TRACER:COMMAND: CRecastContainer::Check Message Is To Large and can not be posted \n" CL_RESET);
Query = "UPDATE phpbb_chat SET count = '1' WHERE message_id = %u;";
Igor's advice is very good - just restrict the SQL query so it only returns results with message size < 120. Simple - do it in the query code so you don't need to do it in C++.
Along these lines, why not also only store valid entries in the database? So if the data doesn't meet the criteria, don't store it in the database to begin with. Remember, with databases, it's garbage in; garbage out.
So if you only store 'clean' data in the database, you've got less work to 'fix-up' the data when you use it later.
Such a simple concept, but many developers miss this and cause themselves a lot of work and more complicated code base because they allow bad data to be stored in the database.