Raised This Month: $32 Target: $400
 8% 

Generate a Random 6 Digit Random ID and Attach on SQL


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
soumyadip77
Senior Member
Join Date: Jul 2017
Location: INDIA,KOLKATA
Old 10-14-2021 , 04:07   Generate a Random 6 Digit Random ID and Attach on SQL
Reply With Quote #1

Hi guys,

I want to create a 6 digit random unique ID for every user this id will be assigned with every steam ID. And stored on the SQL. How can I do this.

I am not good with SQL so.

Thank You
__________________
Let's Help Each Other
soumyadip77 is offline
Send a message via Skype™ to soumyadip77
Celena Luna
Veteran Member
Join Date: Aug 2013
Location: Nagazora
Old 10-14-2021 , 05:47   Re: Generate a Random 6 Digit Random ID and Attach on SQL
Reply With Quote #2

hm.. it sound a bit sus but ok

PHP Code:
/* Sublime AMXX Editor v2.2 */

#include <amxmodx>
#include <sqlx>

#define PLUGIN  "New Plug-In"
#define VERSION "1.0"
#define AUTHOR  "Author"

// Ur Mysql Information
new Host[]     = "hostname"
new User[]    = "username"
new Pass[]     = "password"
new Db[]     = "database"

new Handle:g_SqlTuple
new g_Error[512]

new 
iPlayerID[33]

public 
plugin_init()
{
    
register_plugin(PLUGINVERSIONAUTHOR)

    
set_task(1.0"MySql_Init"// set a task to activate the mysql_init
}

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

public 
client_putinserver(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 playerdb (steamid varchar(32),playerid varchar(6)))")

    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
    
    
//we will now select from the table `playerdb` where the steamid match
    
format(szTemp,charsmax(szTemp),"SELECT * FROM `playerdb` WHERE (`playerdb`.`steamid` = '%s')"szSteamId)
    
SQL_ThreadQuery(g_SqlTuple,"register_client",szTemp,Data,1)
}

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
    id 
Data[0]
    
    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.
        
new rand_num[6]
        for(new 
i=0sizeof(rand_num); i++)
            
rand_num[i] = random(9)
        
format(szTemp,charsmax(szTemp),"INSERT INTO `playerdb` ( `steamid` , `playerid`)VALUES ('%s','%s');",szSteamIdrand_num)
        
SQL_ThreadQuery(g_SqlTuple,"IgnoreHandle",szTemp)
    } 
    else 
    {
        
// if there are results found
        
iPlayerID[id]         = SQL_ReadResult(Query1)
    }
    
    return 
PLUGIN_HANDLED

__________________
My plugin:

Last edited by Celena Luna; 10-14-2021 at 05:56.
Celena Luna is offline
HamletEagle
AMX Mod X Plugin Approver
Join Date: Sep 2013
Location: Romania
Old 10-14-2021 , 05:52   Re: Generate a Random 6 Digit Random ID and Attach on SQL
Reply With Quote #3

Or you know, you could just let SQL generate the ID by using AUTO INCREMENT. Manually generating the random ids has an (extremely small) chance of generating duplicate indexes.
__________________

Last edited by HamletEagle; 10-14-2021 at 05:53.
HamletEagle is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 10-14-2021 , 11:38   Re: Generate a Random 6 Digit Random ID and Attach on SQL
Reply With Quote #4

Provide your table creation statement
__________________
Bugsy is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 10-14-2021 , 23:47   Re: Generate a Random 6 Digit Random ID and Attach on SQL
Reply With Quote #5

CREATE TABLE IF NOT EXISTS `tblPlayers` (`ID` INTEGER PRIMARY KEY AUTOINCREMENT , [the rest of your fields] );
__________________
Bugsy is offline
soumyadip77
Senior Member
Join Date: Jul 2017
Location: INDIA,KOLKATA
Old 10-14-2021 , 23:50   Re: Generate a Random 6 Digit Random ID and Attach on SQL
Reply With Quote #6

Thanks to all will try and update you.
__________________
Let's Help Each Other
soumyadip77 is offline
Send a message via Skype™ to soumyadip77
Reply


Thread Tools
Display Modes

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 14:46.


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