Quote:
Originally Posted by neatek
PHP Code:
Handle StableConnection = null; // Handle? Or Database?
// here, how to check MySQL Connection?
// != null ?! or != INVALID_HANDLE?
|
Check if != null, meaning connection is not nullified aka connected. or check if == null meaning not connected, and return the error.
Handle? Or Database? ->
Database
null or
INVALID_HANDLE ->
null
Also for connection use this little beauty
https://sm.alliedmods.net/new-api/dbi/Database/Connect and for queries
https://sm.alliedmods.net/new-api/dbi/Database/Query
Something like this for reference:
PHP Code:
Database StableConnection = null;
public void OnPluginStart()
{
Database.Connect(OnDatabaseConnect, "my_database_name");
}
public void OnDatabaseConnect(Database idb, const char[] error, any data)
{
if (idb == null || strlen(error) > 0)
{
PrintToServer("[Mysql] Unable to connect to database (%s)", error);
LogError("[Mysql] Unable to connect to database (%s)", error);
return;
}
else
{
//Set to our global variable database named "StableConnection"
StableConnection = idb;
//Do a query of your choice upon connection?
char sQuery[512];
FormatEx(sQuery, sizeof(sQuery), "DELETE FROM my_database_name WHERE map = 'de_dust2'"); //Example (non serious) query, change this to whatever you want...
//Send query
StableConnection.Query(SQL_ErrorCheck, sQuery, _, DBPrio_Normal);
//Log Success Print.
LogMessage("[Mysq] Successfully connected to database!");
}
}
//Callback Function to check for errors
public void SQL_ErrorCheck(Handle owner, DBResultSet results, const char[] error, any data)
{
if (results == null || strlen(error) > 0)
{
LogError("[Mysql] Query failed! %s", error);
}
}
__________________