Raised This Month: $ Target: $400
 0% 

Sql help


Post New Thread Reply   
 
Thread Tools Display Modes
Skyy
AlliedModders Donor
Join Date: Jan 2010
Location: Toronto, Canada
Old 09-04-2011 , 21:26   Re: Sql help
Reply With Quote #11

He wants to know how to create tables in sql properly.

I show example of how to create table:

This tries to create table "multiplier_cat", where the value of "steam_id"
is default (you must still call this value when checking, see below), where
it is of varchar (both char and ints allowed), not null (no default specified),
and this table is sorte by value "steam_id"
PHP Code:
Format(TQuerysizeof(TQuery), "CREATE TABLE IF NOT EXISTS  `multiplier_cat` (`steam_id` varchar(32) NOT NULL, PRIMARY KEY  (`steam_id`)) TYPE=MyISAM;");
    
SQL_TQuery(hDatabaseQueryCreateTableTQuery); 
I show example of how to add column to table:

I tell this to make a change to table named "multiplier_cat", and (attempt)
to add column "xp_timer", using integer of size 32, and setting default
value to 0.
PHP Code:
Format(TQuerysizeof(TQuery), "ALTER TABLE `multiplier_cat` ADD `xp_timer` int(32) NOT NULL DEFAULT '0';");
    
SQL_TQuery(hDatabaseQueryCreateTableTQuery); 
So I show examples of how to save a client into a table:

You notice that I check if AuthString = BOT. This is because bots are
saved with the "BOT" string where a steamid would be. I don't want to
save bots here (I save them somewhere else under a different value)
so if it's a bot (i.e. if the player disconnects in the fragment after this
function is called, and before authstring is fired) we return.

Essayez:
PHP Code:
SaveData(client)
{
    
decl String:Query[256];
    
decl String:Key[64];
    
decl String:Name[256];
    
GetClientAuthString(clientKey65);
    
GetClientName(clientName257);
    if (
StrEqual(Key"BOT")) return;        // BOT. Don't allow.
    
Format(Querysizeof(Query), "REPLACE INTO `multiplier_cat`  (`steam_id`, `xp_timer`) VALUES ('%s', '%d');"Key,  XPMultiplierTime[client]);
    
SQL_TQuery(hDatabaseQuerySetDataQueryclient);

And now how to load data:

I say, search the "multiplier_cat" table, find the "xp_timer" column which
matches "steam_id" of Key (which is the player who is loading, see above)
and then load that data.

PHP Code:
Format(Querysizeof(Query), "SELECT `xp_timer` FROM `multiplier_cat` WHERE (`steam_id` = '%s');"Key);
    
SQL_TQuery(hDatabaseQueryMultiplierDataQueryclient); 
Finally, we have example of the Load Query, as they are all similar.

SQL_FetchInt(hndl, 0); essentially means 0 where the first column of a row
exists to withdraw data.
PHP Code:
public QueryMultiplierDataHandle:ownerHandle:hndl, const String:error[], any:data)

    if ( 
hndl != INVALID_HANDLE )
    {
        while ( 
SQL_FetchRow(hndl) ) 
        {
            
XPMultiplierTime[data] = SQL_FetchInt(hndl0);
        }
    } 
    else
    {
        
LogError"%s"error ); 
        
        return;
    }

You can pull strings as well. You can even call other tables after data has loaded in a specific order if you wish. The only time it becomes some-what
tricky or devilish is when you want to load a string which is an array value.

Such as loading a value where string is simply
new String:SampleString;

would be:

Where "0" is the column position.

PHP Code:
            SQL_FetchString(hndl0SampleStringsizeof(SampleString)); 
However, if the string is defined as in similar:
new String:SampleString[MAXPLAYERS + 1];

You must define a new string for placeholder, and then pass it to the
permanent placeholder. (Where data is of the client)

PHP Code:
decl String:DataInput[64];
            
SQL_FetchString(hndl0DataInputsizeof(DataInput));
            
SampleString[data] = DataInput
Hope that helps. If you have question, feel free to post.
__________________
Skyy is offline
iNexus
Member
Join Date: May 2011
Old 09-05-2011 , 07:29   Re: Sql help
Reply With Quote #12

thank for your help ;)
I'have a question for a select data
How used the selected data? And if credits is not = or > of price , levels is not give Help me
PHP Code:
       public wcs_menu(Handle:menuMenuAction:actionparam1param2)
{
    
    if (
action == MenuAction_Select)
    {
        new 
client GetClientOfUserId(GetEventInt(menu"userid"));
        new 
String:SteamID[255];
        new 
String:credit[255];
        
GetClientAuthString(client,SteamID,sizeof(SteamID));
        
Format(creditsizeof(credit),"SELECT credit FROM warcraft WHERE steamid='%s'",SteamID);
        
SQL_TQuery(hDatabaseQuerySelectDatacreditclient); 
        new 
Handle:Credit SQL_TQuery(hDatabaseQuerySelectDatacreditclient); 
        if (
param2 == 0)
        {
            if (
Credit == 10)
            {
                
ServerCommand("");
                
CPrintToChatclient"{green}[Electik] : {lightgreen}Tu viens de recevoir tes 125 levels.");
            }
            else
            {
                
CPrintToChatclient"{green}[Electik] : {lightgreen}Tu n'as pas assez de credit");
            }
        }
        else if (
param2 == 1)
        {
            if (
Credit == 17)
            {
                
ServerCommand("");
                
CPrintToChatclient"{green}[Electik] : {lightgreen}Tu viens de recevoir tes 250 levels.");
            }
            else
            {
                
CPrintToChatclient"{green}[Electik] : {lightgreen}Tu n'as pas assez de credit");
            }
        }
        else if (
param2 == 2)
        {
            if (
Credit == 17)
            {
                
ServerCommand("");
                
CPrintToChatclient"{green}[Electik] : {lightgreen}Tu viens de recevoir tes 250 levels.");
            }
            else
            {
                
CPrintToChatclient"{green}[Electik] : {lightgreen}Tu n'as pas assez de credit");
            }
        }
    }
    else if (
action == MenuAction_End)
    {
        
CloseHandle(menu);
    }


Last edited by iNexus; 09-05-2011 at 07:45.
iNexus is offline
Skyy
AlliedModders Donor
Join Date: Jan 2010
Location: Toronto, Canada
Old 09-05-2011 , 09:54   Re: Sql help
Reply With Quote #13

I don't know how you have
PHP Code:
SQL_TQuery(hDatabaseQuerySelectDatacreditclient); 
set up, but I believe from what you've pasted you're going about it wrong. Take a look at the code I've pasted for you, it has the answer.

Part of learning is figuring it out on your own. I'll help with part of the equation, but I won't supply the solution.
__________________
Skyy is offline
Mitchell
~lick~
Join Date: Mar 2010
Old 09-05-2011 , 19:50   Re: Sql help
Reply With Quote #14

I would like to implement SQL into one of my mods but this is difficult stuff to learn and not enough free time.. Could some one explain parts of the SQL import and export onto a webserver?
Mitchell is offline
Skyy
AlliedModders Donor
Join Date: Jan 2010
Location: Toronto, Canada
Old 09-06-2011 , 06:11   Re: Sql help
Reply With Quote #15

Quote:
Originally Posted by Mitchell View Post
I would like to implement SQL into one of my mods but this is difficult stuff to learn and not enough free time.. Could some one explain parts of the SQL import and export onto a webserver?

http://wiki.alliedmods.net/SQL_%28So...d_Scripting%29

read. learn. enjoy.
__________________
Skyy is offline
Mitchell
~lick~
Join Date: Mar 2010
Old 09-07-2011 , 21:46   Re: Sql help
Reply With Quote #16

Quote:
Originally Posted by Skyy View Post
Thank you!
Mitchell is offline
Predailien12
Senior Member
Join Date: Feb 2010
Location: Your as* hol*
Old 09-09-2011 , 01:29   Re: Sql help
Reply With Quote #17

haha
this is funny situation
Predailien12 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 18:55.


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