MySQL string fetch/split.

Hey. So, I have a column in my website which is called “Achievements” inside of the “players” table, but they’re bunched together such as this:

1,1,1,1,1,1,1,1,1,1,1,1,1,0,1,1,1,0,0,1,1,1,1,0,1,1,1,1,0,0,1,0,0,0,0,0,0,0,0,1,1,1,0,0,0,1,0,0,0,0,0,1,0,1,

When the value is “1” - it means that the acheivement of the section is unlocked.

For instance:

1,0,0,0,1

The first and fifth item are unlocked and when they’re unlocked, it reads the achievement name from the ID,

So - the first “1” isn’t ID 0, it’s ID 1.

Meaning, it needs to be split from the “,” - but I don’t know how.

Here’s the script version of loading the achievements:

[code]LoadPlayerAchievements(playerid)
{
new achs[MAX_ACHS*2], pos = 0, count = 1;
MySQLFetchString(SQLID[playerid], “Achievements”, achs, “players”);

while(count < MAX_ACHS)
{
    new sub[3];
    strmid(sub, achs, pos, pos+1);

    HasUnlockedAchievement[playerid][count] = strval(sub);

	if(HasUnlockedAchievement[playerid][count] == 1)
	{
	    AchievementPoints[playerid] += AchievementData[count][PointValue];
	}

    pos += 2;
    count ++;
}

return 1;

}
SavePlayerAchievements(playerid)
{
new achs[MAX_ACHS*2];

for(new i = 1; i < MAX_ACHS; i++)
{
	if(HasUnlockedAchievement[playerid][i] == 1)
	{
	    strcat(achs, "1,");
	}else{
	    strcat(achs, "0,");
	}
}

MySQLUpdateString(SQLID[playerid], "Achievements", achs, "players");
return 1;

}[/code]

If anybody can help me that would be amazing. (I want to display the achievement name from the SQL table “Achievements” where I grab the ID and state from the “Achievements” inside of the “players” table.

OMG OMG OMG I HATE delimited columns!!! They create SOME MANY FREAKING ISSUES LATER!!!

MySQL does have a string split function to recover from poor table design, however.

[php]SPLIT_STR(string, delimiter, position)[/php]

Yeah, thank you for the comment. Made me convert it into a different type of loading.

I have now got it inside of a separate MySQL table “playerach” and it stores as the table:

id(AUTO_INCREMENT) | PlayerSQLID | AchievementID

For example:

5 | 1 | 24

That way, I can call it from PHP as:

$query = mysqli_query($connectionInfo, "SELECT AchName FROM achievements WHERE id=$AchievementID"); while($row = mysqli_fetch_array($query)){ echo $row["AchName"]; }

That way, it’s a lot quicker and much smoother.

Much better design.

Sponsor our Newsletter | Privacy Policy | Terms of Service