Raised This Month: $62 Target: $400
 15% 

Solved sql in amx ?


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
nacknic
Senior Member
Join Date: Mar 2019
Old 01-24-2021 , 12:03   sql in amx ?
Reply With Quote #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.
nacknic is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 01-24-2021 , 16:55   Re: sql in amx ?
Reply With Quote #2

So what is your question?
__________________
fysiks is offline
nacknic
Senior Member
Join Date: Mar 2019
Old 01-25-2021 , 10:57   Re: sql in amx ?
Reply With Quote #3

Quote:
Originally Posted by fysiks View Post
So what is your question?
i cant see the result where is the problem? or where im wrong ?
nacknic is offline
Black Rose
Veteran Member
Join Date: Feb 2011
Location: Upplands Väsby, Sweden
Old 01-25-2021 , 13:04   Re: sql in amx ?
Reply With Quote #4

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)
__________________

Last edited by Black Rose; 01-25-2021 at 13:05.
Black Rose is offline
Natsheh
Veteran Member
Join Date: Sep 2012
Old 01-25-2021 , 13:37   Re: sql in amx ?
Reply With Quote #5

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.
__________________
@Jailbreak Main Mod v2.6.5 100%
@User Tag Prefix 100% done !
@Mystery Box 100% done !
@VIP System 100% done !

Last edited by Natsheh; 01-25-2021 at 14:04.
Natsheh is offline
Send a message via MSN to Natsheh Send a message via Skype™ to Natsheh
nacknic
Senior Member
Join Date: Mar 2019
Old 01-25-2021 , 16:42   Re: sql in amx ?
Reply With Quote #6

Quote:
Originally Posted by Natsheh View Post
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 is offline
nacknic
Senior Member
Join Date: Mar 2019
Old 01-26-2021 , 06:59   Re: sql in amx ?
Reply With Quote #7

Quote:
Originally Posted by Black Rose View Post
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

Last edited by nacknic; 01-26-2021 at 07:18.
nacknic is offline
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 20:51.


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