Raised This Month: $ Target: $400
 0% 

Mysql- getting number of entries.


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
naven
Veteran Member
Join Date: Jun 2008
Location: Poland, Cieszyn
Old 01-03-2013 , 06:19   Mysql- getting number of entries.
Reply With Quote #1

I have been playing with mysql for quite some time, but i can't find solution for this one anywhere.
Basically i want to check how many entries are in the database for player's steamid.
PHP Code:
new liczSQL_ReadResult(Query1//here is the log error 
This gives me an error, how do I do it properly?

Modified code from some tutorial i was using for testing (it gives me an error that column is not right)
PHP Code:
#include <amxmodx>
#include <sqlx>

#define PLUGIN "Maps completed"
#define VERSION "1.0a"
#define AUTHOR "Grim"

// Ur Mysql Information
new Host[]     = "naven.com.pl"
new User[]    = "naven1_surf"
new Pass[]     = "xxx"
new Db[]     = "naven1_surf"

new Handle:g_SqlTuple
new g_Error[512]


new 
iExp[33]

public 
plugin_init() {
    
register_plugin(PLUGINVERSIONAUTHOR)
    
set_task(1.0"MySql_Init"// set a task to activate the mysql_init
    
register_clcmd("say check","check")
}
public 
check(id)
{
    
Load_MySql(id)
}
public 
MySql_Init()
{
    
// we tell the API that this is the information we want to connect to,
    // just not yet. basically it's like storing it in global variables
    
g_SqlTuple SQL_MakeDbTuple(Host,User,Pass,Db)
    
    
// ok, we're ready to connect
    
new ErrorCode,Handle:SqlConnection SQL_Connect(g_SqlTuple,ErrorCode,g_Error,charsmax(g_Error))
    if(
SqlConnection == Empty_Handle)
        
// stop the plugin with an error message
    
set_fail_state(g_Error)
    
    new 
Handle:Queries
    
// we must now prepare some random queries
    
Queries SQL_PrepareQuery(SqlConnection,"CREATE TABLE IF NOT EXISTS surf (name varchar(34), czas double, fps INT(5), mapa varchar(64),steamid varchar(64))")
    
    if(!
SQL_Execute(Queries))
    {
        
// if there were any problems
        
SQL_QueryError(Queries,g_Error,charsmax(g_Error))
        
set_fail_state(g_Error)
        
    }
    
    
// close the handle
    
SQL_FreeHandle(Queries)
    
    
// you free everything with SQL_FreeHandle
    
SQL_FreeHandle(SqlConnection)   
}

public 
plugin_end()
{
    
// free the tuple - note that this does not close the connection,
    // since it wasn't connected in the first place
    
SQL_FreeHandle(g_SqlTuple)
}

public 
Load_MySql(id)
{
    new 
szSteamId[32], szTemp[512]
    
get_user_authid(idszSteamIdcharsmax(szSteamId))
    
    new 
Data[1]
    
Data[0] = id
    
new licz;
    
    
//we will now select from the table `tutorial` where the steamid match
    //format(szTemp,charsmax(szTemp),"SELECT * FROM `tutorial` WHERE (`tutorial`.`steamid` = '%s')", szSteamId)
    
format(szTemp,charsmax(szTemp),"SELECT count(*) from `surf` WHERE `steamid` = '%s'"szSteamId)
    
SQL_ThreadQuery(g_SqlTuple,"register_client",szTemp,Data,1)
    
//client_print(id, print_chat, "Licz: %i", licz)
    //client_print(id, print_chat, "o: %s", Data)
}

public 
register_client(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)
    }
    
    new 
id//, licz[512] ;
    
id Data[0]
    
client_print(idprint_chat"%s"Data)
    
/*while(SQL_NumResults(Query) > 1)
    {
        licz ++;
        client_print(id, print_chat, "%i", licz)
        client_print(id, print_chat, "%s", Query)
    }*/
    
if(SQL_NumResults(Query) < 1
    {
        
//.if there are no results found
        
        
new szSteamId[32]
        
get_user_authid(idszSteamIdcharsmax(szSteamId)) // get user's steamid
        
        //  if its still pending we can't do anything with it
        
if (equal(szSteamId,"ID_PENDING"))
            return 
PLUGIN_HANDLED
        
        
//new szTemp[512]
        
        // now we will insturt the values into our table.
        //format(szTemp,charsmax(szTemp),"INSERT INTO `tutorial` ( `steamid` , `exp`)VALUES ('%s','0');",szSteamId)
        //SQL_ThreadQuery(g_SqlTuple,"IgnoreHandle",szTemp)
    

    else 
    {
        new 
liczSQL_ReadResult(Query1//here is the log error
        
        
client_print(idprint_chat"%i"licz)
        
// if there are results found
        //iExp[id]         = SQL_ReadResult(Query, 1)
    
}
    
    return 
PLUGIN_HANDLED
}
//SQL_ReadResult(Query, SQL_FieldNameToNum(Query, "pierwsza"), szText, 127);
/*public Save_MySql(id)
{
    new szSteamId[32], szName[32], szTemp[512]
    get_user_authid(id, szSteamId, charsmax(szSteamId))
    
    // Here we will update the user hes information in the database where the steamid matches.
    format(szTemp,charsmax(szTemp),"UPDATE `tutorial` SET `exp` = '%i' WHERE `tutorial`.`steamid` = '%s';",iExp[id], szSteamId)
    SQL_ThreadQuery(g_SqlTuple,"IgnoreHandle",szTemp)
}*/

public IgnoreHandle(FailState,Handle:Query,Error[],Errcode,Data[],DataSize)
{
    
SQL_FreeHandle(Query)
    
    return 
PLUGIN_HANDLED
}

public 
client_putinserver(id)
{
    
Load_MySql(id)
}

public 
client_disconnect(id)
{
    
//Save_MySql(id)

Is this
PHP Code:
format(szTemp,charsmax(szTemp),"SELECT count(*) from `surf` WHERE `steamid` = '%s'"szSteamId
Correct?
__________________
naven.com.pl
"At the end of the day, there are always going to be mental disorders and people who cause violence for no other reason than the fact that they're fucked up and lost. And all we can do is try to learn from it." Corey Taylor.

Last edited by naven; 01-03-2013 at 06:20.
naven is offline
DjOptimuS
Senior Member
Join Date: Jan 2009
Old 01-03-2013 , 07:01   Re: Mysql- getting number of entries.
Reply With Quote #2

Dunno if this is what you are looking for but what I understand is this: You want to check how many entries you have in the database.

Why don't you make an ID column in the database, with autoincrement, and read that through MySQL.

More info here http://www.w3schools.com/sql/sql_autoincrement.asp

I believe this method is more resource consuming efficient.

Last edited by DjOptimuS; 01-03-2013 at 07:01.
DjOptimuS is offline
naven
Veteran Member
Join Date: Jun 2008
Location: Poland, Cieszyn
Old 01-03-2013 , 07:29   Re: Mysql- getting number of entries.
Reply With Quote #3

There are 6 rows
name map steamid fps time date
I want to check how many maps has been completed by player with certain steamid
__________________
naven.com.pl
"At the end of the day, there are always going to be mental disorders and people who cause violence for no other reason than the fact that they're fucked up and lost. And all we can do is try to learn from it." Corey Taylor.

Last edited by naven; 01-03-2013 at 07:30.
naven is offline
Sylwester
Veteran Member
Join Date: Oct 2006
Location: Poland
Old 01-03-2013 , 08:03   Re: Mysql- getting number of entries.
Reply With Quote #4

name map steamid fps time date are columns not rows.
With the query you are using (select count(*) from...) you will get result with only 1 column and the field numbers start at 0 not 1.
Code:
SQL_ReadResult(Query, 0)
__________________
Impossible is Nothing
Sylwester is offline
naven
Veteran Member
Join Date: Jun 2008
Location: Poland, Cieszyn
Old 01-03-2013 , 08:51   Re: Mysql- getting number of entries.
Reply With Quote #5

Quote:
Originally Posted by Sylwester View Post
name map steamid fps time date are columns not rows.
ok
Quote:
Code:
SQL_ReadResult(Query, 0)
It seems to be working fine, thank you.
__________________
naven.com.pl
"At the end of the day, there are always going to be mental disorders and people who cause violence for no other reason than the fact that they're fucked up and lost. And all we can do is try to learn from it." Corey Taylor.
naven is offline
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 13:28.


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