AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Mysql handle returning 0 on query problem (https://forums.alliedmods.net/showthread.php?t=24610)

SaintK 02-27-2006 08:40

Mysql handle returning 0 on query problem
 
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...

Brad 02-27-2006 08:56

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

SaintK 02-27-2006 09:12

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]

Brad 02-27-2006 09:15

What line sparks that error?

SaintK 02-27-2006 09:17

Quote:

Originally Posted by Brad
What line sparks that error?

Code:
result = dbi_query(dbc,"select * from `savednames` where name = '%s'", clientname)

Sandurr 02-27-2006 09:44

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 }

SaintK 02-27-2006 09:52

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:51:37: [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 02-27-2006 11:40

ok this is a little more info on the issue
 
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 - 16:33:15: [MYSQL] Invalid result handle 0
L 02/27/2006 - 16:33:15: [AMXX] Displaying debug trace (plugin "sqltest.amxx")
L 02/27/2006 - 16:33:15: [AMXX] Run time error 10: native error (native "dbi_num_rows")
L 02/27/2006 - 16:33: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.

Sandurr 02-27-2006 12:00

put

Code:
steamid='%s'

not

Code:
'steamid'='%s'

Xanimos 02-27-2006 12:02

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)


All times are GMT -4. The time now is 20:17.

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