Raised This Month: $ Target: $400
 0% 

Mysql handle returning 0 on query problem


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
SaintK
Veteran Member
Join Date: Mar 2004
Old 02-27-2006 , 08:40   Mysql handle returning 0 on query problem
Reply With Quote #1

getting a strange callback could any one see if i am doing it wrong

Code:
#include <amxmodx>
#include <amxmisc>
#include <dbi>

new clientname[32]
new usersteam[32]

new user_id[32]
new user_steamid[32]
		
new Sql:dbc 
new Result:result
new query[256]

new error[33]

new num 
new i


public client_authorized(id){
	
	get_user_name(id,clientname,31)	
	get_user_authid(id,usersteam,31)
	
	
	result = dbi_query(dbc,"select * from `savednames` where name = '%s'", clientname)
	num = dbi_num_rows(result)
	i = 1
	
	while ( i < num )
	{  
         dbi_nextrow(result)
	dbi_result(result,"steamid", user_steamid, 31)
	dbi_free_result(result)	
	dbi_result(result,"name", user_id, 31)
	dbi_free_result(result)
	i++;
	}
			       
				
	if (  contain(user_id,clientname) && contain(user_steamid,usersteam)   )
	
	{
	//client_print(id, print_chat, "Welcome to our server.  We've Preserved your information.  Enjoy your stay.")
	//server_print("[Logger-ClientAuth] Logged into database: %s (%s).", clientname, usersteam)
	server_print("[Logger-ClientAuth-1] Logged into database: %s (%s).", user_id, user_steamid)	
	return PLUGIN_HANDLED 
	} 
	
	else if(result == RESULT_NONE)
    {
       server_print("[Logger-ClientAuth-1] ERROR. User(%s)(%s) does not exist in database.", user_id, user_steamid)
	return PLUGIN_HANDLED 
    }
    
	else
	
	{
	client_print(id, print_chat, "You are not allowd to use this name please chose another !!!")
	server_cmd("kick %d This name cannot be used",id)
	client_cmd(id , "disconnect") 
	}
	
	return PLUGIN_HANDLED 
	
	}
the databse table has ID steamid name

the query error is
02/27/2006 - 13:13:44: [MYSQL] Invalid database handle 0
02/27/2006 - 13:13:44: [AMXX] Displaying debug trace (plugin "NickProtect.amxx")
02/27/2006 - 13:13:44: [AMXX] Run time error 10: native error (native "dbi_query")
02/27/2006 - 13:13:44: [AMXX] [0] NickProtect.sma::client_authorized (line 53)
Dropped sucks from server
Reason: Client sent 'drop'

i am presumeing that its returning 0 on the rows wich just shouldnt be true as there is one name in it can any one see anything wrong here...
SaintK is offline
Brad
AMX Mod X Team Member
Join Date: Jun 2004
Old 02-27-2006 , 08:56  
Reply With Quote #2

That's not your whole plugin. Can you post the entire thing and enclose it in the [small ] tags?

Having said that, you need to check the result from a query to see if it failed. Your code presumes it will always succeed.

Ex:
Code:
//If the query is less than 0, it failed        if (result < RESULT_NONE) {         new err[255]         new errNum = dbi_error(mysql, err, 254)         server_print("error2: %s|%d", err, errNum)     }
Brad is offline
SaintK
Veteran Member
Join Date: Mar 2004
Old 02-27-2006 , 09:12  
Reply With Quote #3

Code:
#include <amxmodx> #include <amxmisc> #include <dbi> #define PLUGIN "Nick Protection" #define VERSION "1.0" #define AUTHOR "[DumB]SteamSucks" new clientname[32] new usersteam[32] new user_id[32] new user_steamid[32]         new Sql:dbc new Result:result new query[256] new error[33] new num new i public plugin_init() {     register_plugin(PLUGIN, VERSION, AUTHOR)         dbc = dbi_connect("localhost", "root", "", "dk", error, 32)     if (dbc == SQL_FAILED)         {         server_print("[AMXX] Could Not Connect To SQL Database. Check your login details.")         return PLUGIN_HANDLED         }     result = dbi_query(dbc,"CREATE TABLE IF NOT EXISTS `savednames` (`id` int,`steamid` varchar(30), `name` varchar(30), PRIMARY KEY(`id`))")     server_print("[AMXX] Connected to SQL Database, logging all users.")     dbi_free_result(result)     return PLUGIN_CONTINUE } public client_authorized(id){         get_user_name(id,clientname,31)     get_user_authid(id,usersteam,31)         result = dbi_query(dbc,"select * from `savednames` where name = '%s'", clientname)          if (result < RESULT_NONE) {           new err[255]           new errNum = dbi_error(dbc, err, 254)           server_print("error2: %s|%d", err, errNum)                                         }           num = dbi_num_rows(result)     i = 1         while ( i < num )     {            dbi_nextrow(result)     dbi_result(result,"steamid", user_steamid, 31)     dbi_free_result(result)     dbi_result(result,"name", user_id, 31)     dbi_free_result(result)     i++;     }                                         if (  contain(user_id,clientname) && contain(user_steamid,usersteam)   )         {     server_print("[Logger-ClientAuth-1] Logged into database: %s (%s).", user_id, user_steamid)     return PLUGIN_HANDLED     }         else if(result == RESULT_NONE)     {        server_print("[Logger-ClientAuth-1] ERROR. User(%s)(%s) does not exist in database.", user_id, user_steamid)     return PLUGIN_HANDLED     }         else         {     client_print(id, print_chat, "You are not allowd to use this name please chose another !!!")     server_cmd("kick %d This name cannot be used",id)     client_cmd(id , "disconnect")     }         return PLUGIN_HANDLED         }

the error comes back as

Adding master server 68.142.72.250:27010
Adding master server 69.28.151.162:27010
L 02/27/2006 - 14:09:22: [MYSQL] Invalid database handle 0
L 02/27/2006 - 14:09:22: [AMXX] Displaying debug trace (plugin "NickProtect.amxx")
L 02/27/2006 - 14:09:22: [AMXX] Run time error 10: native error (native "dbi_query")
L 02/27/2006 - 14:09:22: [AMXX] [0]
SaintK is offline
Brad
AMX Mod X Team Member
Join Date: Jun 2004
Old 02-27-2006 , 09:15  
Reply With Quote #4

What line sparks that error?
Brad is offline
SaintK
Veteran Member
Join Date: Mar 2004
Old 02-27-2006 , 09:17  
Reply With Quote #5

Quote:
Originally Posted by Brad
What line sparks that error?
Code:
result = dbi_query(dbc,"select * from `savednames` where name = '%s'", clientname)
SaintK is offline
Sandurr
Senior Member
Join Date: Aug 2005
Old 02-27-2006 , 09:44  
Reply With Quote #6

you can't put clientname at the end of dbi_query()

Use format() to fix this.

Example:

Code:
new saint[256] format(saint,255,"SELECT * FROM `savednames` WHERE name = '%s'", clientname) result = dbi_query(dbc,saint) if(dbi_nextrow(result) > 0) {     // ... connection successful or no names in database }
Sandurr is offline
SaintK
Veteran Member
Join Date: Mar 2004
Old 02-27-2006 , 09:52  
Reply With Quote #7

Quote:
Originally Posted by Sandurr
you can't put clientname at the end of dbi_query()

Use format() to fix this.

Example:

Code:
new saint[256] format(saint,255,"SELECT * FROM `savednames` WHERE name = '%s'", clientname) result = dbi_query(dbc,saint) if(dbi_nextrow(result) > 0) {     // ... connection successful or no names in database }

still same error


L 02/27/2006 - 14:517: [MYSQL] Invalid database handle 0

Code:
new checkid[256] format(checkid,255,"SELECT * FROM `savednames` WHERE steamid = '%s'", usersteam) dbi_query(dbc,checkid) result = dbi_query(dbc,checkid) /*<- error code here */

seems to be anoyed at the result code for some reason
SaintK is offline
SaintK
Veteran Member
Join Date: Mar 2004
Old 02-27-2006 , 11:40   ok this is a little more info on the issue
Reply With Quote #8

i changed the code abit so i could see better what was going on

Code:
get_user_name(id,clientname,31)     get_user_authid(id,usersteam,31)     get_user_name(id,user_id,31)         server_print("[Logger-ClientAuth-1] ClientSteamid is: %s",usersteam)     server_print("[Logger-ClientAuth-1] ClientName is: %s",clientname)         new checkid[500]     format(checkid,499,"SELECT * FROM `savednames` WHERE `steamid` = '%s'", usersteam)       server_print("[Logger-ClientAuth-1] Sending sql string %s",checkid)     result = dbi_query(dbc,checkid)

in the console i got back


couldn't exec banned.cfg
Adding master server 69.28.151.162:27010
Adding master server 207.173.177.11:27010
[Logger-ClientAuth-1] ClientSteamid is: STEAM_ID_LAN
[Logger-ClientAuth-1] ClientName is: [DumB]SteamSucks
[Logger-ClientAuth-1] Sending sql string SELECT * FROM `savednames` WHERE `steamid` = 'STEAM_ID_LAN'
L 02/27/2006 - 163:15: [MYSQL] Invalid result handle 0
L 02/27/2006 - 163:15: [AMXX] Displaying debug trace (plugin "sqltest.amxx")
L 02/27/2006 - 163:15: [AMXX] Run time error 10: native error (native "dbi_num_rows")
L 02/27/2006 - 163:15: [AMXX] [0] sqltest.sma::client_authorized (line 70)

as you can see the actual select string was correct and in mysql i used the same string and got back one result so why is amx mod telling me that the result handle is 0 ie no results when it truely is a hit
here is mysql direct result below

Showing rows 0 - 0 (1 total, Query took 0.0006 sec) SQL-query:
SELECT *
FROM `savednames`
WHERE `steamid` = 'STEAM_ID_LAN'
LIMIT 0 , 30

any more ideas people before this totally gives me a brain melt down.
SaintK is offline
Sandurr
Senior Member
Join Date: Aug 2005
Old 02-27-2006 , 12:00  
Reply With Quote #9

put

Code:
steamid='%s'

not

Code:
'steamid'='%s'
Sandurr is offline
Xanimos
Veteran Member
Join Date: Apr 2005
Location: Florida
Old 02-27-2006 , 12:02  
Reply With Quote #10

Your problem lies within
Code:
    while ( i < num )     {            dbi_nextrow(result)     dbi_result(result,"steamid", user_steamid, 31)     dbi_free_result(result)         dbi_result(result,"name", user_id, 31)     dbi_free_result(result)     i++;     }

You are not supposed to free the result untill after you parse everything out of it so make it
Code:
    while ( i < num )     {            dbi_nextrow(result)          dbi_result(result,"steamid", user_steamid, 31)          dbi_result(result,"name", user_id, 31)          i++;     }     dbi_free_result(result)
Xanimos is offline
Send a message via AIM to Xanimos Send a message via MSN to Xanimos
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 20:17.


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