AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   SQL Query Result Help (https://forums.alliedmods.net/showthread.php?t=25858)

nhdriver4 03-22-2006 02:27

SQL Query Result Help
 
Im writing a plugin, and I cant seem to get my sql querys to return any results.

Code:
    new name[32],authid[35]     get_user_authid(id,authid,35)     get_user_name(id,name,32)     new query[350]     format(query,350,"SELECT steamid,tag FROM `%s` WHERE steamid = '%s' ",sql_table,authid)     new Result:result = dbi_query(dbc,query)

I created a log_amx line with the same variables below. It's picking up the table name fine. That's defined at the top of the document. My problem is trying to get the authid to show up with the users steam id in the query.

Any ideas?

v3x 03-22-2006 02:41

Make all of your lengths minus one ( except where you declare the new variables ).

nhdriver4 03-22-2006 03:03

Quote:

Originally Posted by v3x
Make all of your lengths minus one ( except where you declare the new variables ).

Thanks for the suggestion, but still didn't work

Here's the code:
Code:
new name[32],authid[35]     get_user_authid(id,authid,35)     get_user_name(id,name,32)         new query[350]     format(query,350,"SELECT steamid,tag FROM `%s` WHERE steamid ='%s'",sql_table,authid)     log_amx("SELECT steamid,tag FROM `%s` WHERE steamid = '%s'",sql_table,authid)     new Result:result = dbi_query(dbc,query)

Here's the log:
Code:

L 03/22/2006 - 03:04:31: [tagline.amxx] SELECT steamid,tag FROM `tagline` WHERE steamid = ''
L 03/22/2006 - 03:04:31: [tagline.amxx] [AMXXSQL] Database returned no results


Any other ideas?

teame06 03-22-2006 03:29

Show us the full code

nhdriver4 03-22-2006 03:45

Sure. Dont mind the mess. It's my first plug using a database.

Code:
#include <amxmodx> #include <amxmisc> #include <engine> #include <dbi> #define PLUGIN "SQL Tagline" #define VERSION "0.1" #define AUTHOR "Wuss" //Sql_Parameter #define sql_host  "localhost"   // Host Name #define sql_user  "root"    // User Name #define sql_pass  ""        // Password #define sql_dbase   "amxx"      // Datbase Name #define sql_table  "tagline"    // The name of the table to create for tagline storage new Sql:dbc; new Result:result; new qerror[128]; new g_TagsON = 1; public plugin_init() {     register_plugin(PLUGIN, VERSION, AUTHOR)     register_concmd("amx_tagline","tagCreate",ADMIN_RESERVATION,"<message> - Creates a tagline that's displayed when you connect to the server")     check_connection() //  create_table()     } // Attempt the Connection public check_connection() {         dbc = dbi_connect(sql_host,sql_user, sql_pass, sql_dbase, qerror,128);         // Verify our database connection has worked     if ( dbc == SQL_OK )     {         log_amx( "[AMXXSQL] Connection to %s database successful",sql_host);         return PLUGIN_CONTINUE;             }     else     {         log_amx( "[AMXXSQL] Connection to %s database failed",sql_host);     }     return PLUGIN_HANDLED } // Make the table if it doesn't exist // NOT WORKING YET /* public create_table() {     new Result:ret = dbi_query(dbc,"CREATE TABLE IF NOT EXISTS `%s` (`steamid` VARCHAR(35) NOT NULL DEFAULT '',`tag` VARCHAR(256) NOT NULL DEFAULT '')",sql_table)         if ( ret < RESULT_NONE )     {         log_amx( "[AMXXSQL] Database not created")         return PLUGIN_CONTINUE;     }     return PLUGIN_HANDLED } */ public tagCreate(id) {     new authid[64],tag[256],name[32];     get_user_authid(id,authid,63);     get_user_name(id,name,31);     read_argv(1,tag,255)     new Result:res = dbi_query(dbc,"REPLACE INTO %s VALUES ( '%s','%s' )",sql_table,authid,tag);         if (res < RESULT_NONE)     {         log_amx("Error creating a tag entry for %s",name);         console_print(id,"Sorry %s, it didn't work!",name);         return PLUGIN_HANDLED     }         dbi_free_result(res);     console_print(id,"[AMXX] Tagline for %s inserted into database.",authid);     return PLUGIN_HANDLED } // This is the routnie that displays the tagline upon connecting public client_authorized(id) {     set_task(5.0,"tagDisplay") } public tagDisplay(id) {     new name[32],authid[35]     get_user_authid(id,authid,35)     get_user_name(id,name,32)         new query[351]     format(query,351,"SELECT steamid,tag FROM `%s` WHERE steamid ='%s'",sql_table,authid)     log_amx("SELECT steamid,tag FROM `%s` WHERE steamid = '%s'",sql_table,authid)     new Result:result = dbi_query(dbc,query)             if (result == RESULT_FAILED)     {         log_amx("[AMXXSQL] Database connection failed")         return PLUGIN_HANDLED     }         else if (result == RESULT_NONE)     {         log_amx("[AMXXSQL] Database returned no results")         return PLUGIN_HANDLED     }         if(g_TagsON)     {         new rsteamid[35], rtag[256]         dbi_field(Result,1,rsteamid,35)         dbi_field(Result,2,rtag,256)         dbi_free_result(result)                 if(g_TagsON)         {             client_print(0,print_chat,"[AMXX] %s: %s",name,rtag)             log_amx( "[AMXXSQL] %s %s",name,rtag)         }     }     else     {         client_print(0,print_chat,"No tagline is defined for %s",name)         log_amx("[AMXXSQL] No tageline is defined for %s",name)     }         } /* /////////////////////////////////////////////////////////////// public client_disconnect(id){     new szQuery[256],name[32]         new authid[64],ip[32]     get_user_authid(id,authid,63)     get_user_name(id,name,31)     get_user_ip(id,ip,31)     format(szQuery, 511, "REPLACE INTO `%s` (`steamid`,`name`,`ip`, `time`) VALUES ('%s','%s','%s', 'NOW()')", sql_table,authid,name,ip);         new Result:res = dbi_query(sql, szQuery)         if (res < RESULT_NONE)         {         log_amx("Error in querying database, location: %d", id);     }         dbi_free_result(res); }

teame06 03-22-2006 04:01

Your not passing the id...

Code:
set_task(5.0,"tagDisplay", id)


When you do only set_task(5.0,"tagDisplay") not passing any number. It will be zero.

nhdriver4 03-22-2006 04:51

Fixed my problem perfect. Thanks.

It now retrieved the information from the databse. However, I now receive this error:
Code:

L 03/22/2006 - 04:52:39: [MYSQL] Invalid result handle -1
L 03/22/2006 - 04:52:39: [AMXX] Displaying debug trace (plugin "tagline.amxx")
L 03/22/2006 - 04:52:39: [AMXX] Run time error 10: native error (native "dbi_nextrow")
L 03/22/2006 - 04:52:39: [AMXX]    [0] tagline.sma::tagDisplay (line 118)

There can only be one result. Am I using the wrong format here?

Xanimos 03-22-2006 05:20

Quote:

Originally Posted by nhdriver4
Fixed my problem perfect. Thanks.

It now retrieved the information from the databse. However, I now receive this error:
Code:

L 03/22/2006 - 04:52:39: [MYSQL] Invalid result handle -1
L 03/22/2006 - 04:52:39: [AMXX] Displaying debug trace (plugin "tagline.amxx")
L 03/22/2006 - 04:52:39: [AMXX] Run time error 10: native error (native "dbi_nextrow")
L 03/22/2006 - 04:52:39: [AMXX]    [0] tagline.sma::tagDisplay (line 118)

There can only be one result. Am I using the wrong format here?


change
Code:
if(g_TagsON)
to
Code:
else if(g_TagsON)

nhdriver4 03-22-2006 05:26

The tag still shows, along with the error :cry:

Sandurr 03-22-2006 05:29

be sure ur connected to the database


All times are GMT -4. The time now is 16:46.

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