Hi everyone, i have a problem i live in halls, and i do not have any databases and internet is terrible . i cannot connect to any free mysql bases. Who can look on my code and say, how i can improve it, and it is look good ? because i didn't work with mysql ever
Some of the code
PHP Code:
public void OnPluginStart(/*void*/)
{
if(SQL_CheckConfig(AMMOPACK_DATABASE_STRING))
{
// Initialize some chars
char sError[BIG_LINE_LENGTH];
char sRequest[BIG_LINE_LENGTH];
char sDriver[SMALL_LINE_LENGTH];
// Creates an SQL connection from a named configuration
DataBase = SQL_Connect(AMMOPACK_DATABASE_STRING, false, sError, sizeof(sError));
// If base doesn't exist or mod can't make connection
if(DataBase == NULL)
SetFailState("[ZBM3] ERROR:[007] Can't connect to MySQL server (%s)\n\n", sError);
// Reads the driver of an opened database
SQL_ReadDriver(DataBase, sDriver, SMALL_LINE_LENGTH);
// If driver is not a MySQL
if(!StrEqual(sDriver, "mysql", false))
SetFailState("[ZBM3] ERROR:[008] Driver of connection must to be a MySQL. Edit databases.cfg\n\n");
// Block mysql base from other requests
SQL_LockDatabase(DataBase);
// Create database
Format(sRequest, BIG_LINE_LENGTH, "CREATE TABLE IF NOT EXISTS `zp_ammopacks_table` \
(`id` int(11) NOT NULL auto_increment, \
`steam` varchar(31) NOT NULL, \
`ammopacks` int(11) NOT NULL, \
PRIMARY KEY (`id`), \
UNIQUE KEY `steam` (`steam`))");
SQL_FastQuery(DataBase, sRequest);
// Unblock mysql base
SQL_UnlockDatabase(DataBase);
}
else
SetFailState("[ZBM3] ERROR:[009] Section %s doesn't found in databases.cfg\n\n", AMMOPACK_DATABASE_STRING);
}
public void OnClientPostAdminCheck(int clientIndex)
{
gDataID[clientIndex] = -1;
GetClientAuthId(clientIndex, AuthId_Steam2, gSteamID[clientIndex], sizeof(gSteamID));
// Initialize char
char sRequest[BIG_LINE_LENGTH];
Format(sRequest, BIG_LINE_LENGTH, "SELECT id, ammopacks FROM zp_ammopacks_table WHERE steam = '%s'", gSteamID[clientIndex]);
SQL_TQuery(DataBase, MySQL_ClientConnected, sRequest, GetClientUserId(clientIndex));
}
public void OnClientDisconnect(int clientIndex)
{
// Initialize char
char sRequest[BIG_LINE_LENGTH];
if( gDataID[ clientIndex ] == -1)
Format(sRequest, BIG_LINE_LENGTH, "UPDATE zp_ammopacks_table SET ammopacks = %i WHERE steam = '%s'", iAmmoPack, gSteamID[clientIndex]);
else
Format(sRequest, sBIG_LINE_LENGTH, "UPDATE zp_ammopacks_table SET ammopacks = %i WHERE id = %i", iAmmoPack, gDataID[ clientIndex ]);
SQL_TQuery(DataBase, MySQL_ClientDisconnected, sRequest);
}
public void MySQL_ClientConnected(Handle sParent, Handle sHandle, const char[] sError, any clientID)
{
// Get real player index from event key
int clientIndex = GetClientOfUserId(clientID);
// Make sure the client didn't disconnect while the thread was running
if (!clientIndex || gSteamID[clientIndex][0] != 'S')
{
return;
}
// If invalid query handle
if (sHandle == NULL)
{
LogError("[ZBM3] ERROR:[010] Query load fail: %s", sError);
return;
}
// Client not found, add him into the MySQL Base
if (!SQL_GetRowCount(sHandle))
{
// Initialize char
char sRequest[BIG_LINE_LENGTH];
// Create request
Format(sRequest, BIG_LINE_LENGTH, "INSERT INTO zp_ammopacks_table(steam) VALUES('%s')", gSteamID[clientIndex]);
SQL_FastQuery(DataBase, sRequest);
return;
}
// Client was found in the table
if(SQL_FetchRow(sHandle))
{
gDataID[clientIndex] = SQL_FetchInt(sHandle, 0); // Load client ID
gAmmoPacks[clientIndex] = SQL_FetchInt(sHandle, 1); // Load client ammopacks
}
}
public void MySQL_ClientDisconnected(Handle sParent, Handle sHandle, const char[] sError, any data)
{
// If invalid query handle
if (sHandle == NULL)
{
LogError("[ZBM3] ERROR:[011] Query save fail: %s", sError);
return;
}
}
__________________