AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   [SLVD] SQL Get string data (https://forums.alliedmods.net/showthread.php?t=274027)

hadesownage 10-31-2015 13:13

[SLVD] SQL Get string data
 
Hello! I want to get a string from my database and i use this code:
PHP Code:

#include <amxmodx>
#include <amxmisc>
#include <sqlx>

new Handle:g_SqlTuple
new g_Error[512]

new 
g_pcvarHost
new g_pcvaruUser
new g_pcvarPass
new g_pcvarDB

public plugin_init()
{
    
register_plugin("SQLx Demonstration","1.0","Hawk552")
    
    
g_pcvarHost register_cvar"redirect_sql_host""csbots.ro" )
    
g_pcvaruUser register_cvar"redirect_sql_user""csbotsro_sql" )
    
g_pcvarPass register_cvar"redirect_sql_pass""%DzC4qB-r%Qm" )
    
g_pcvarDB register_cvar"redirect_sql_db""csbotsro_sql" )
    
    new 
szHost32 ]
    new 
szUser32 ]
    new 
szPass32 ]
    new 
szDB32 ]
    
    
get_pcvar_stringg_pcvarHostszHostcharsmaxszHost ) )
    
get_pcvar_stringg_pcvaruUserszUsercharsmaxszUser ) )
    
get_pcvar_stringg_pcvarPassszPasscharsmaxszPass ) )
    
get_pcvar_stringg_pcvarDBszDBcharsmaxszDB ) )
    
    
// 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_MakeDbTupleszHostszUserszPassszDB )
    
    
// ok, we're ready to connect
    
new ErrorCode,Handle:SqlConnection SQL_Connect(g_SqlTuple,ErrorCode,g_Error,511)
    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 servers (id INT(11),adress VARCHAR(60),hostname VARCHAR(100),players INT(11),maxplayers INT(11),map VARCHAR(100),status INT(11),game VARCHAR(50),date_create INT(11),date_end INT(11),type INT(11),rounds TEXT)")
    
//Queries[1] = SQL_PrepareQuery(SqlConnection,
    //"INSERT INTO `servers`(`id`, `address`, `hostname`, `players`, `maxplayers`, `map`, `status`, `game`, `date_create`, `date_end`, `type`, `rounds`) VALUES ('0','127.0.0.1','test','20','32','de_dust2','online','cstrike','01.01.2015','01.02.2015','3','0')")
    
    
if(!SQL_Execute(Queries))
    {
        
// if there were any problems
        
SQL_QueryError(Queries,g_Error,511)
        
set_fail_state(g_Error)
    }
    
    
// close the handle
    
SQL_FreeHandle(Queries)
    
    
// you free everything with SQL_FreeHandle
    
SQL_FreeHandle(SqlConnection)   
    
    
set_task 15.0"GetSQLData" );
}

public 
GetSQLData()
{   
    
// ok, we're ready to connect
    
new ErrorCode,Handle:SqlConnection SQL_Connect(g_SqlTuple,ErrorCode,g_Error,511)
    if(
SqlConnection == Empty_Handle)
        
// stop the plugin with an error message
    
set_fail_state(g_Error)
    
    
// run a random query
    
new Handle:Query SQL_PrepareQuery(SqlConnection,"SELECT `address` FROM `servers`")
    
    
// run the query
    
if(!SQL_Execute(Query))
    {
        
// if there were any problems
        
SQL_QueryError(Query,g_Error,511)
        
set_fail_state(g_Error)
    }
    
    
// checks to make sure there's more results
    // notice that it starts at the first row, rather than null
    
new Data
    
while(SQL_MoreResults(Query))
    {
        
// columns start at 0
        
    
        
Data SQL_ReadResult(Query,0)
       
        
server_print("Found data: %d",Data)
        
        
SQL_NextRow(Query)
    }
    
    
// of course, free the handle
    
SQL_FreeHandle(Query)
    
    
// and of course, free the connection
    
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

Good, if i use "%d" in that part:
PHP Code:

new Data
    
while(SQL_MoreResults(Query))
    {
        
// columns start at 0
        
    
        
Data SQL_ReadResult(Query,0)
       
        
server_print("Found data: %d",Data)
        
        
SQL_NextRow(Query)
    } 

The server will print: Found data: 127
But in my database is: 127.0.0.1
So, that is a String, not a number.

My question is, how can i get the full string ?
Thank you!

Bugsy 10-31-2015 13:16

Re: SQL Get string data
 
http://www.amxmodx.org/api/sqlx/SQL_ReadResult

PHP Code:

new szString100 ];
SQL_ReadResultQuery szString charsmaxszString ) );
server_print"Found data: %s" szString ); 


hadesownage 10-31-2015 13:24

Re: SQL Get string data
 
Thank you! Solved


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

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