View Single Post
muukis
Veteran Member
Join Date: Apr 2009
Old 09-02-2009 , 04:17   Re: [L4D] Player Stats (Co-op) v1.1.1
Reply With Quote #608

I have a theory on why the UTF8 is not always working...

At the plugin start, this gets processed:
PHP Code:
    // Init MySQL connections
    
ConnectDB(); 
... like it should be before the event hooking, but in the ConnectDB function:
PHP Code:
// Make connection to database.

public ConnectDB()
{
    if (
SQL_CheckConfig("l4dstats"))
    {
        new 
String:Error[256];
        
db SQL_Connect("l4dstats"trueErrorsizeof(Error));

        if (
db == INVALID_HANDLE)
            
LogError("Failed to connect to database: %s"Error);
        else
            
SendSQLUpdate("SET NAMES 'utf8'");
    }
    else
        
LogError("Database.cfg missing 'l4dstats' entry!");

... the connection is created nonthreaded like it should be, but then the UTF8 encoding is set in SendSQLUpdate function:
PHP Code:
// Run a SQL query, used for UPDATE's only.

public SendSQLUpdate(String:query[])
{
    if (
db == INVALID_HANDLE)
        return;

    
SQL_TQuery(dbSQLErrorCheckCallbackquery);

... in a threaded query.

What this means is that there is a window of opportunity for a query or possibly even queries to be made before the UTF8 is actually set? Tell me if I'm completely lost here?

My solution would be this:
PHP Code:
// Make connection to database.

public ConnectDB()
{
    if (
SQL_CheckConfig("l4dstats"))
    {
        new 
String:Error[256];
        
db SQL_Connect("l4dstats"trueErrorsizeof(Error));

        if (
db == INVALID_HANDLE)
            
LogError("Failed to connect to database: %s"Error);
        else if (!
SQL_FastQuery(db"SET NAMES 'utf8'"))
        {
            if (
SQL_GetError(dbErrorsizeof(Error)))
                
LogError("Failed to update encoding to UTF8: %s"Error);
            else
                
LogError("Failed to update encoding to UTF8: unknown");
        }
    }
    else
        
LogError("Database.cfg missing 'l4dstats' entry!");

I have not tested or even compiled this. It was just a thought.

Last edited by muukis; 09-02-2009 at 05:43.
muukis is offline