Raised This Month: $51 Target: $400
 12% 

[SLVD] SQL Get string data


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
hadesownage
AlliedModders Donor
Join Date: Jun 2013
Location: Romania, Iași
Old 10-31-2015 , 13:13   [SLVD] SQL Get string data
Reply With Quote #1

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!

Last edited by hadesownage; 10-31-2015 at 13:25. Reason: Solved
hadesownage is offline
Send a message via Yahoo to hadesownage Send a message via Skype™ to hadesownage
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 10-31-2015 , 13:16   Re: SQL Get string data
Reply With Quote #2

http://www.amxmodx.org/api/sqlx/SQL_ReadResult

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

Last edited by Bugsy; 10-31-2015 at 13:17.
Bugsy is offline
hadesownage
AlliedModders Donor
Join Date: Jun 2013
Location: Romania, Iași
Old 10-31-2015 , 13:24   Re: SQL Get string data
Reply With Quote #3

Thank you! Solved
hadesownage is offline
Send a message via Yahoo to hadesownage Send a message via Skype™ to hadesownage
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 02:18.


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