Senior Member
|
01-24-2021
, 12:03
sql in amx ?
|
#1
|
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
Last edited by nacknic; 01-26-2021 at 08:04.
|
|