AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Problem with Varchars (https://forums.alliedmods.net/showthread.php?t=140726)

Schwabba 10-16-2010 03:38

Problem with Varchars
 
PHP Code:

public Toplist(id)
{
    for(new 
igMaxClientsi++)
    {
        if(
is_user_connected(i))
        {
            
Save_MySql(i// Save all stats to get the correct rank
        
}
    }

    new 
Data[1]
    
Data[0] = id

    
new szTemp[512]

    for(new 
i16i++)
    {
        new 
up -1
        format
(szTemp,charsmax(szTemp),"SELECT * FROM `deathrun` ORDER BY `deathrun`.`points` DESC LIMIT %i , %i"upi)
        
SQL_ThreadQuery(g_SqlTuple,"Toplist2",szTemp,Data,1)
    }

    return 
PLUGIN_CONTINUE
}

public 
Toplist2(FailState,Handle:Query,Error[],Errcode,Data[],DataSize)
{
    if(
FailState == TQUERY_CONNECT_FAILED)
    {
        
log_amx("Load - Could not connect to SQL database.  [%d] %s"ErrcodeError)
    }
    else if(
FailState == TQUERY_QUERY_FAILED)
    {
        
log_amx("Load Query failed. [%d] %s"ErrcodeError)
    }

    if(
SQL_ReadResult(Query,0) || SQL_ReadResult(Query,0) == 0)
    {

        new 
countcount1count2count3count4count5

        count 
SQL_ReadResult(Query,0)
        
count1 SQL_ReadResult(Query,1)
        
count2 SQL_ReadResult(Query,2)
        
count3 SQL_ReadResult(Query,3)
        
count4 SQL_ReadResult(Query,4)
        
count5 SQL_ReadResult(Query,5)

        if(
count == 0)
        
count 1

        print_color
(010"^x04-=[Pingstars]=- ^x01 %s - %s - %i - %i - %i - %i "countcount1count2count3count4count5)
    }

    return 
PLUGIN_HANDLED


returns

-=[Pingstars]=- - - 951 - 39 - 125 - 1
-=[Pingstars]=- - - 0 - 0 - 0 - 0

mysql entries:

steamid name points lives frags
STEAM_0:0:16065469 Pingstars * Schwabba 951 39 125 1
BOT [P*D]Bruce_Lee (99) 0 0 0 0

can't get the varchars.. dunno why..

when i change

PHP Code:

if(SQL_ReadResult(Query,0) || SQL_ReadResult(Query,0) == 0

to

PHP Code:

if(SQL_ReadResult(Query,0)) 

then i get no results

Sylwester 10-16-2010 04:35

Re: Problem with Varchars
 
It would be strange if it worked. After all you are trying to store string in a single variable. Try this:
PHP Code:

        new count[32], count1[32], count2count3count4count5

        SQL_ReadResult
(Query,0count31)
        
SQL_ReadResult(Query,1count131)
        
count2 SQL_ReadResult(Query,2)
        
count3 SQL_ReadResult(Query,3)
        
count4 SQL_ReadResult(Query,4)
        
count5 SQL_ReadResult(Query,5

and remove that:
PHP Code:

        if(count == 0)
        
count 

About the 2nd problem:
PHP Code:

if(SQL_ReadResult(Query,0) || SQL_ReadResult(Query,0) == 0

will give you the same result as
PHP Code:

if(true

Both of them are wrong anyway. You should change it to:
PHP Code:

if(SQL_MoreResults(Query)) 

Your way of creating toplist is very inefficient.
Why do you send 15 queries to mysql to get the toplist when you could get it with only 1 query?

I would code it this way:
1) Load toplist from mysql and store it in memory on plugin_init
2) Load player stats and store them in memory on client_authorized
3) Update toplist stored in memory when player gains points
4) Display toplist stored in memory when player wants to see toplist
5) Save player stats on client_disconnect

Schwabba 10-16-2010 09:30

Re: Problem with Varchars
 
Quote:

It would be strange if it worked. After all you are trying to store string in a single variable. Try this:
PHP Code:

        new count[32], count1[32], count2count3count4count5

        SQL_ReadResult
(Query,0count31)
        
SQL_ReadResult(Query,1count131)
        
count2 SQL_ReadResult(Query,2)
        
count3 SQL_ReadResult(Query,3)
        
count4 SQL_ReadResult(Query,4)
        
count5 SQL_ReadResult(Query,5


OMG, i coded the whole night, looks like i was to sleepy to use my brain.^^

Sometimes the simplest things took the most time...

Quote:

and remove that:

PHP Code:

        if(count == 0)
        
count 


Yep, forgot to delete it (used it on the rank system).

Quote:

About the 2nd problem:

PHP Code:

if(SQL_ReadResult(Query,0) || SQL_ReadResult(Query,0) == 0

will give you the same result as
PHP Code:

if(true

Both of them are wrong anyway. You should change it to:
PHP Code:

if(SQL_MoreResults(Query)) 


I know that, i just added the 2nd if, because it does'nt worked and i don't have to rewrite the 1st if later.

Quote:

Your way of creating toplist is very inefficient.
Why do you send 15 queries to mysql to get the toplist when you could get it with only 1 query?

I would code it this way:
1) Load toplist from mysql and store it in memory on plugin_init
2) Load player stats and store them in memory on client_authorized
3) Update toplist stored in memory when player gains points
4) Display toplist stored in memory when player wants to see toplist
5) Save player stats on client_disconnect
Then i have to make 15 queries on plugin_init? xD

I gotta sleep now (looks like i'm not able to code currently) xD

Sylwester 10-16-2010 09:58

Re: Problem with Varchars
 
No you make 1 query in plugin init and retrieve 15 rows at once:
PHP Code:

public load_toplist()
{
    new 
szTemp[512]
    
format(szTemp,charsmax(szTemp),"SELECT * FROM `deathrun` ORDER BY `deathrun`.`points` DESC LIMIT 15")
    
SQL_ThreadQuery(g_SqlTuple,"Toplist2",szTemp)
    return 
PLUGIN_CONTINUE
}

public 
Toplist2(FailState,Handle:Query,Error[],Errcode,Data[],DataSize)
{
    if(
FailState)
    {
        
log_amx("SQL Error: %s (%d)"ErrorErrcode)
        return 
PLUGIN_HANDLED
    
}

    new 
count[32], count1[32], count2count3count4count5
    
while(SQL_MoreResults(Query))
    {
        
SQL_ReadResult(Query,0count31)
        
SQL_ReadResult(Query,1count131)
        
count2 SQL_ReadResult(Query,2)
        
count3 SQL_ReadResult(Query,3)
        
count4 SQL_ReadResult(Query,4)
        
count5 SQL_ReadResult(Query,5)

        
print_color(010"^x04-=[Pingstars]=- ^x01 %s - %s - %i - %i - %i - %i "countcount1count2count3count4count5)
        
SQL_NextRow(Query)
    }

    return 
PLUGIN_HANDLED


You can test this with clcmd.

Schwabba 10-16-2010 18:51

Re: Problem with Varchars
 
Ah, thank you very much.

I never heared about SQL_NextRow, but it's very useful.


All times are GMT -4. The time now is 10:17.

Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.