AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Solved sql in amx ? (https://forums.alliedmods.net/showthread.php?t=330207)

nacknic 01-24-2021 12:03

sql in amx ?
 
fixed!
Code:

#include <amxmodx>
#include <sqlx>
 
new Handle:SQL_Tuple
new Handle:SQL_Connection
public plugin_init() {
        register_clcmd( "say /getusers", "sql_con" )
}
public sql_con(id) {
    if(!(get_user_flags(id) & ADMIN_KICK)) return client_print(0,print_chat,"you have no access")
    SQL_Tuple = SQL_MakeDbTuple("00.000.000.000","root","","test",120) //not show my ip
    new err, error[256]
    SQL_Connection = SQL_Connect(SQL_Tuple, err, error, charsmax(error))
    if(SQL_Connection == Empty_Handle) client_print(0,print_chat,"%s",error)
    else sql_select(id)
}
public sql_select(id){
    client_print(0,print_chat,"connect to db")
    new ip[21]
    new user[21]
    get_user_ip(id,ip,charsmax(ip),1)
    new Handle:query = SQL_PrepareQuery(SQL_Connection,"SELECT username FROM `clients` WHERE `ip`= '%s'",ip)
    if(SQL_Execute(query)) {
            if(SQL_NumResults(query) > 0) {
                new columNumOfUsername = SQL_FieldNameToNum(query,"username")
                if(columNumOfUsername >= 0) {
                        SQL_ReadResult(query,columNumOfUsername,user,charsmax(user))
                        client_print(0,print_chat,"%s",user)
                } else client_print(0,print_chat,"cant find username column")
        } else client_print(0,print_chat,"not fount results")
    } else client_print(0,print_chat,"cant execute the query")
    SQL_FreeHandle(SQL_Connection)       
}

if i have more results (more then one row with same ip)
Code:

#include <amxmodx>
#include <sqlx>
 
new Handle:SQL_Tuple
new Handle:SQL_Connection
public plugin_init() {
        register_clcmd( "say /getusers", "sql_con" )
}
public sql_con(id) {
    if(!(get_user_flags(id) & ADMIN_KICK)) return client_print(0,print_chat,"you have no access")
    SQL_Tuple = SQL_MakeDbTuple("00.000.000","root","","test",120)
    new err, error[256]
    SQL_Connection = SQL_Connect(SQL_Tuple, err, error, charsmax(error))
    if(SQL_Connection == Empty_Handle) client_print(0,print_chat,"%s",error)
    else sql_select(id)
}
public sql_select(id){
    client_print(0,print_chat,"connect to db")
    new ip[21]
    new user[21][21]
    get_user_ip(id,ip,charsmax(ip),1)
    new q[] = "SELECT username FROM `test`.`clients` WHERE `ip`= '%s';"
    new Handle:query = SQL_PrepareQuery(SQL_Connection,q,ip)
    if(SQL_Execute(query)) {
            new howManyRowsReturned = SQL_NumResults(query)
            if(howManyRowsReturned  > 0) {
                client_print(0,print_chat," found %d users:",howManyRowsReturned)
                new columNumOfUsername = SQL_FieldNameToNum(query,"username")
                if(columNumOfUsername >= 0) {
                        for(new i=0; i<howManyRowsReturned; i++) {
                                SQL_ReadResult(query,columNumOfUsername,user,charsmax(user))
                                client_print(0,print_chat,"client # %d = %s",i+1,user)
                                SQL_NextRow(query)
                        }
                } else client_print(0,print_chat,"cant find username column")
        } else client_print(0,print_chat,"not fount results")
    } else client_print(0,print_chat,"cant execute the query")
    SQL_FreeHandle(SQL_Connection)       
}

see pic database:
https://ibb.co/NTKSBh7

fysiks 01-24-2021 16:55

Re: sql in amx ?
 
So what is your question?

nacknic 01-25-2021 10:57

Re: sql in amx ?
 
Quote:

Originally Posted by fysiks (Post 2734149)
So what is your question?

i cant see the result where is the problem? or where im wrong ?

Black Rose 01-25-2021 13:04

Re: sql in amx ?
 
You're gonna kick yourself.

Code:
    if(SQL_Connection != Empty_Handle) client_print(0,print_chat,"%s",error)
    else sql_select(id)
->
Code:
    if(SQL_Connection == Empty_Handle) client_print(0,print_chat,"%s",error)
    else sql_select(id)

Natsheh 01-25-2021 13:37

Re: sql in amx ?
 
I've experienced a problem lately with selecting from the the table, if i didn't define which database i was selecting from, therefore try changing your query to.

Code:

"SELECT username FROM `test`.`clients` WHERE `ip`= '%s'";
and remember to add ; on the end for each query you send.

nacknic 01-25-2021 16:42

Re: sql in amx ?
 
Quote:

Originally Posted by Natsheh (Post 2734246)
I've experienced a problem lately with selecting from the the table, if i didn't define which database i was selecting from, therefore try changing your query to.

Code:

"SELECT username FROM `test`.`clients` WHERE `ip`= '%s'";
and remember to add ; on the end for each query you send.

thank i will check tomorrow

nacknic 01-26-2021 06:59

Re: sql in amx ?
 
Quote:

Originally Posted by Black Rose (Post 2734245)
You're gonna kick yourself.

Code:
    if(SQL_Connection != Empty_Handle) client_print(0,print_chat,"%s",error)
    else sql_select(id)
->
Code:
    if(SQL_Connection == Empty_Handle) client_print(0,print_chat,"%s",error)
    else sql_select(id)

hahaha thank you, it was late at night when i was wrote this shit.
and after spend 1h i was manage to fix xampp problems.
and because the db on my xampp on my pc and the server far from me i set timout to 120sec in SQL_MakeDbTuple


All times are GMT -4. The time now is 21:15.

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