MySQL Strings
#1

Hello, I'm making a phone system and I'm trying to get the Message value from DB. With integer characters I don't have that problem, works fine but with the string I'm having a problem.

Let's suppose I have 4 messages:

Message 1: Hello
Message 2: Good job
Message 3: Nice
Message 4: Works

When I try to see message 1, the string shows:
HGNWorks
When I try to see message 2, the string shows:
GNWorks
When I try to see message 3, the string shows:
NWorks
When I try to see message 4, the string shows:
Works

Seems like load the first word of every message and the complete the message with the last message.

Here is my code:

Код:
forward LoadUserSMS(playerid, phone);
public LoadUserSMS(playerid, phone)
{
	new query[240];
	mysql_format(handle, query, sizeof(query), "SELECT * FROM `SMS` WHERE `Phone` = '%d'", phone);
	mysql_tquery(handle, query, "OnSmsUserLoad", "id", playerid, telefono);
	return 1;
}

forward OnSmsUserLoad(playerid, phone);
public OnSmsUserLoad(playerid, phone)
{
    new total;
    for(new i = 0; i < cache_num_rows(); i++)
	{
	    if(total < 15)
	    {
			pSMS[playerid][ID][i] = cache_get_field_content_int(i, "ID");
			pSMS[playerid][Telep][i] = cache_get_field_content_int(i, "Phone");
			pSMS[playerid][From][i] = cache_get_field_content_int(i, "From");
			cache_get_field_content(i, "Message", pSMS[playerid][Message][i], .max_len = 128);
			pSMS[playerid][Stats][i] = cache_get_field_content_int(i, "Stats");
			total++;
			if(pSMS[playerid][Stats][i] == 2)
			{
			    SendClientMessage(playerid, -1, "You have unread messages.");
			}
  		}
	}
}

//Command viewmessages:

new sms[1024];
for(new i = 0; i < MAX_SMS; i++)
			{
			    if(MyNumber[playerid] == pSMS[playerid][Phone][i])
			    {
			        new status[30];
			        status = "new";
			        if(pSMS[playerid][Stats][i] == 1) format(status, sizeof(status), "new");
			        format(sms, sizeof(sms), "%sFrom %i (%s)\n", sms, pSMS[playerid][From][i], status);
   }
			}
	        ShowPlayerDialog(playerid, DIALOG_PHONE_SMS, DIALOG_STYLE_LIST, "My messages", sms, "View", "Close");

// On dialog response
if(dialogid == DIALOG_PHONE_SMS)
	{
	    if(!response) return 1;
	    new title[100];
	    new message[128];
	    ActionSMS[playerid] = pSMS[playerid][ID][listitem];
	    format(title, sizeof(title), "From #%i", pSMS[playerid][From][listitem]);
	    format(message, sizeof(message), "%s", pSMS[playerid][Message][listitem]);
	    new query[400];
	    mysql_format(handle, query, sizeof(query), "UPDATE `SMS` SET Stats= '1' WHERE ID = '%d'", ActionSMS[playerid]);
		mysql_query(handle, query);
	    ShowPlayerDialog(playerid, DIALOG_PHONE_SMS2, DIALOG_STYLE_MSGBOX, title, message, "Delete", "Save");
	    return 1;
	}
if(dialogid == DIALOG_PHONE_SMS2)
	{
	    if(response)
	    {
	        new query[400];
	        mysql_format(handle, query, sizeof(query), "DELETE FROM `SMS` WHERE ID = '%d'", ActionSMS[playerid]);
			mysql_query(handle, query);
	    	ActionSMS[playerid] = 0;
	    	ResetPhonesUser(playerid);
			LoadUserPhones(playerid);
		}
		else
		{
                    ActionSMS[playerid] = 0;
		    ResetPhonesUser(playerid);
			LoadUserPhones(playerid);
		    return 1;
		}
		return 1;
	}
Tried some new code but nothing.. ANYONE PLEASE?
Reply
#2

Looking the MySQL Log, I see the data loaded it's correct. I think the problem is something about displaying it.

May these help:

Код:
#define MAX_SMS 15 // Max SMS x player

enum SMS
{
	ID,
	Telep,
	From,
	Message[128],
	Stats
};

new pSMS[MAX_PLAYERS][SMS][MAX_SMS];
Reply
#3

Hmm, this is confusing. I understand what you're trying to do but I'm not sure if it can be solved. Perhaps if you swap SMS and MAX_SMS. This line is obviously the culprit:
PHP код:
cache_get_field_content(i"Message"pSMS[playerid][Message][i], .max_len 128); 
Because here i refers to the index of Message, not to the index of SMS. If that makes sense. So when row 0 is retrieved it writes "Hello" into message starting at index 0. When row 1 is retrieved it writes "Good job" into message starting at index 1, thereby overwriting what was already there. So the string is now "HGood job". This continues for however many rows there are.
Reply
#4

Yeah but if I make a printf or SendClientMessage in OnSmsUserLoad, it shows the message.
From MySQL the data is retrived okay, because these shows the message fine:

Код:
public OnSmsUserLoad(playerid, telefono)
{
	new id, loaded;
    while(loaded < cache_get_row_count())
	{
	    id = cache_get_field_content_int(loaded, "ID");
	   	pSMS[playerid][ID][loaded] = id;
		pSMS[playerid][Telep][loaded] = cache_get_field_content_int(loaded, "Phone");
		pSMS[playerid][From][loaded] = cache_get_field_content_int(loaded, "From");
		cache_get_field_content(loaded, "Message", pSMS[playerid][Message][loaded]);
		pSMS[playerid][Stats][loaded] = cache_get_field_content_int(loaded, "Stats");
		SendClientMessage(playerid, -1, pSMS[playerid][Message][loaded]);
		loaded++;
	}
}
I think the problem comes when I try to call pSMS[playerid][Message][loaded], for example:

Код:
pSMS[playerid][Phone][1]; // Shows number OK
pSMS[playerid][Message][1]; // Shows message incorrectly.
Reply
#5

Quote:

mysql_format(handle, query, sizeof(query), "SELECT * FROM `SMS` WHERE `Phone` = '%d'", phone);
mysql_tquery(handle, query, "OnSmsUserLoad", "id", playerid, telefono)

I might be wrong but I see something wrong here.... Don't you?
Reply
#6

Replace:
pawn Код:
new pSMS[MAX_PLAYERS][SMS][MAX_SMS];
to:
pawn Код:
new pSMS[MAX_PLAYERS][MAX_SMS][SMS];
and swap all the rest as well, for example this:
pawn Код:
pSMS[playerid][Message][listitem]
becomes:
pawn Код:
pSMS[playerid][listitem][Message]
and so on. The sms-id first and last the item of the enumerator.
Reply
#7

Is translated from Spanish, sorry, the real code is:

Код:
mysql_format(handle, query, sizeof(query), "SELECT * FROM `SMS` WHERE `Phone` = '%d'", phone);
mysql_tquery(handle, query, "OnSmsUserLoad", "id", playerid, phone);
Reply
#8

Quote:
Originally Posted by Konstantinos
Посмотреть сообщение
Replace:
pawn Код:
new pSMS[MAX_PLAYERS][SMS][MAX_SMS];
to:
pawn Код:
new pSMS[MAX_PLAYERS][MAX_SMS][SMS];
and swap all the rest as well, for example this:
pawn Код:
pSMS[playerid][Message][listitem]
becomes:
pawn Код:
pSMS[playerid][listitem][Message]
and so on. The sms-id first and last the item of the enumerator.
Thanks Konstantinos it worked, can you explain me why it happens?
Reply
#9

Quote:
Originally Posted by HidroDF
Посмотреть сообщение
Thanks Konstantinos it worked, can you explain me why it happens?
It most likely ignored the last dimension (sms) and used the index of "Message" as that.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)