<div style="margin:20px; margin-top:5px">
<div class="smallfont" style="margin-bottom:2px">PHP код:</div>
<div class="alt2" dir="ltr" style="
margin: 0px;
padding: 6px;
border: 1px solid #C2C2C2;
width: 800px;
height: 178px;
text-align: left;
overflow: auto">
<code style="white-space:nowrap">
<!-- php buffer start --><code><span style="color: #000000">
<span style="color: #0000BB">forward callBackFunction</span><span style="color: #007700">(</span><span style="color: #0000BB">playerid</span><span style="color: #007700">);
<br/>public </span><span style="color: #0000BB">callBackFunction</span><span style="color: #007700">(</span><span style="color: #0000BB">playerid</span><span style="color: #007700">)
<br/>{
<br/>
<br/> </span><span style="color: #0000BB">Faction</span><span style="color: #007700">[</span><span style="color: #0000BB">playerid</span><span style="color: #007700">][</span><span style="color: #0000BB">ID</span><span style="color: #007700">] = </span><span style="color: #0000BB">cache_insert_id</span><span style="color: #007700">();
<br/> </span><span style="color: #0000BB">cache_get_field_content</span><span style="color: #007700">(</span><span style="color: #0000BB">0</span><span style="color: #007700">,</span><span style="color: #DD0000">"factionname"</span><span style="color: #007700">,</span><span style="color: #0000BB">Faction</span><span style="color: #007700">[</span><span style="color: #0000BB">playerid</span><span style="color: #007700">][</span><span style="color: #0000BB">FactionName</span><span style="color: #007700">]);
<br/> </span><span style="color: #0000BB">ShowPlayerDialog</span><span style="color: #007700">(</span><span style="color: #0000BB">playerid</span><span style="color: #007700">, </span><span style="color: #0000BB">factiontyped</span><span style="color: #007700">, </span><span style="color: #0000BB">DIALOG_STYLE_LIST</span><span style="color: #007700">, </span><span style="color: #DD0000">"FACTION CREATION"</span><span style="color: #007700">, </span><span style="color: #DD0000">"Type 0(Drugs)\nType 1(Weapons)"</span><span style="color: #007700">, </span><span style="color: #DD0000">"Choose"</span><span style="color: #007700">,</span><span style="color: #DD0000">"Cancel"</span><span style="color: #007700">);
<br/> </span><span style="color: #0000BB">printf</span><span style="color: #007700">(</span><span style="color: #DD0000">"[INFO] New faction created. Database ID: [%d]"</span><span style="color: #007700">, </span><span style="color: #0000BB">Faction</span><span style="color: #007700">[</span><span style="color: #0000BB">playerid</span><span style="color: #007700">][</span><span style="color: #0000BB">ID</span><span style="color: #007700">]);
<br/>}
<br/></span><span style="color: #0000BB"></span>
</span>
</code><!-- php buffer end -->
</code>
</div>
</div>I'm trying to access Faction[playerid][Factionname] to a individual player like this.
<div style="margin:20px; margin-top:5px">
<div class="smallfont" style="margin-bottom:2px">PHP код:</div>
<div class="alt2" dir="ltr" style="
margin: 0px;
padding: 6px;
border: 1px solid #C2C2C2;
width: 800px;
height: 50px;
text-align: left;
overflow: auto">
<code style="white-space:nowrap">
<!-- php buffer start --><code><span style="color: #000000">
<span style="color: #0000BB"></span><span style="color: #007700">[</span><span style="color: #0000BB">PHP</span><span style="color: #007700">]</span><span style="color: #0000BB">mysql_format</span><span style="color: #007700">(</span><span style="color: #0000BB">mysql</span><span style="color: #007700">,</span><span style="color: #0000BB">query</span><span style="color: #007700">,</span><span style="color: #0000BB">sizeof</span><span style="color: #007700">(</span><span style="color: #0000BB">query</span><span style="color: #007700">), </span><span style="color: #DD0000">"UPDATE `accounts` SET `factionname` = %e `factionid` = %d WHERE ID = %d LIMIT 1"</span><span style="color: #007700">, </span><span style="color: #0000BB">Faction</span><span style="color: #007700">[</span><span style="color: #0000BB">playerid</span><span style="color: #007700">][</span><span style="color: #0000BB">FactionName</span><span style="color: #007700">], </span><span style="color: #0000BB">Faction</span><span style="color: #007700">[</span><span style="color: #0000BB">playerid</span><span style="color: #007700">][</span><span style="color: #0000BB">ID</span><span style="color: #007700">], </span><span style="color: #0000BB">Player</span><span style="color: #007700">[</span><span style="color: #0000BB">playerid</span><span style="color: #007700">][</span><span style="color: #0000BB">ID</span><span style="color: #007700">]);
<br/> </span><span style="color: #0000BB">mysql_tquery</span><span style="color: #007700">(</span><span style="color: #0000BB">mysql</span><span style="color: #007700">,</span><span style="color: #0000BB">query</span><span style="color: #007700">,</span><span style="color: #DD0000">""</span><span style="color: #007700">,</span><span style="color: #DD0000">""</span><span style="color: #007700">);
<br/></span><span style="color: #0000BB"></span>
</span>
</code><!-- php buffer end -->
</code>
</div>
</div>
mysql_tquery("INSERT INTO...", "callBackFunction", "i", playerid);
format(Faction[playerid][FactionName], SIZE, name);
case factionnamed:
{
new query[500];
mysql_format(mysql,query,sizeof(query), "INSERT INTO `faction` (`factionname`) VALUES ('%e')",inputtext);
mysql_tquery(mysql, query, "callBackFunction", "i",playerid);
Player[playerid[FactionName] = inputtext[120];
mysql_format(mysql,query,sizeof(query), "UPDATE `accounts` SET `factionname` = %e `factionid` = %d WHERE ID %d LIMIT 1", Player[playerid][FactionName], Faction[playerid][ID], Player[playerid][ID]);
mysql_tquery(mysql,query,"","");
}
CMD:createfaction(playerid,params[])
{
if(Player[playerid][Admin] < 4) return SendClientMessage(playerid, COLOR_GREY, "You must be an adminstrator to use this command");
ShowPlayerDialog(playerid, factionnamed, DIALOG_STYLE_INPUT, "Faction Creation", "Enter the faction name", "Next","Cancel");
return 1;
}
enum PlayerData
{
ID,
Name[MAX_PLAYER_NAME],
Password[129],
IP[16],
Admin,
VIP,
FactionID,
FactionName[200],
Money,
Float:posX,
Float:posY,
Float:posZ,
Float:posA
};
new Player[MAX_PLAYERS][PlayerData];
enum FactionData
{
ID,
factionname[200],
factiontype
};
new Faction[MAX_PLAYERS][FactionData];
case factionnamed:
{
new bool:success = true;
format(Player[playerid[FactionName], 120, inputtext);
new query[200];
mysql_format(mysql, query, sizeof (query), "INSERT INTO `faction` (`factionname`) VALUES ('%e')", inputtext);
if (mysql_tquery(mysql, query) == 0)
success = false;
mysql_format(mysql, query, sizeof (query), "UPDATE `accounts` SET `factionname` = '%e', `factionid` = %d WHERE `ID` = %d LIMIT 1", Player[playerid][FactionName], Faction[playerid][ID], Player[playerid][ID]);
if (mysql_tquery(mysql, query) == 0)
success = false;
if (success)
{
// Every thing went well
ShowPlayerDialog(playerid, factiontyped, DIALOG_STYLE_LIST, "FACTION CREATION", "Type 0(Drugs)\nType 1(Weapons)", "Choose","Cancel");
}
else
{
// Either a new row in "faction" table wasn't created or `accounts` wasn't updated
}
}
[..] and you don't need to use a callback for that, because mysql_tquery returns 1 or 0 for success and fail.
|
A query (referring to INSERT) can fail for multiple reasons (duplicate, column that does not exist, column count does not match value count etc.) but the function will return success either way as long as the connection handle is valid.
As for the grave accent character is not mandatory and reduces the size of the query if not used. |
case factionnamed:
{
new bool:success = true;
format(Player[playerid][FactionName], 120, inputtext);
new query[200];
mysql_format(mysql, query, sizeof (query), "INSERT INTO `faction` (`factionname`) VALUES ('%e')", inputtext);
if (mysql_tquery(mysql, query) == 0)
success = false;
mysql_format(mysql, query, sizeof (query), "UPDATE `accounts` SET `factionname` = '%e', `factionid` = %d WHERE `ID` = %d LIMIT 1", Player[playerid][FactionName], Faction[playerid][ID], Player[playerid][ID]);
if (mysql_tquery(mysql, query) == 0)
success = false;
if (success)
{
Faction[playerid][ID] = cache_insert_id();
printf("[INFO] New faction created. Database ID: [%d]", Faction[playerid][ID]);
ShowPlayerDialog(playerid, factiontyped, DIALOG_STYLE_LIST, "FACTION CREATION", "Type 0(Drugs)\nType 1(Weapons)", "Choose","Cancel");
}
else
{
// Either a new row in "faction" table wasn't created or `accounts` wasn't updated
}
}
case factiontyped:
{
if(response)
{
switch(listitem)
{
//mysql_format(mysql,query,sizeof(query), "UPDATE `accounts` SET `Admin` = %d WHERE ID = %d LIMIT 1", level, Player[target][ID]);
case 0:
{
new query[500];
new str[250];
format(str,sizeof(str), "FACTION CREATED %s", Player[playerid][FactionName]);
SendClientMessage(playerid,COLOR_GREY,str);
SendClientMessage(playerid, COLOR_GREY, "[INFO] FACTION CREATED!(Drug Warehouse)");
Player[playerid][FactionID] = Faction[playerid][ID];
mysql_format(mysql, query,sizeof(query), "UPDATE `faction` SET `factiontype` = '0' WHERE ID = %d LIMIT 1", Player[playerid][FactionID]);
mysql_tquery(mysql, query, "", "");
}
case 1:
{
new query[500];
SendClientMessage(playerid, COLOR_GREY, "[INFO] FACTION CREATED!(Gun Warehouse)");
Player[playerid][FactionID] = Faction[playerid][ID];
mysql_format(mysql, query,sizeof(query), "UPDATE `faction` SET `factiontype` = '1' WHERE ID = %d LIMIT 1",Player[playerid][FactionID]);
mysql_tquery(mysql, query, "", "");
}
}
}
}