AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   MySQL Prob (https://forums.alliedmods.net/showthread.php?t=2272)

Freecode 05-31-2004 06:06

MySQL Prob
 
Its 4 am here and im proly making some stupid mistake.

I have a table thats setup like this
steamid | balance | income

here is my code. It is not getting the income data. Always shows 0 when its set to 4.
Code:
public pay() {     client_print(0,3,"Test:1");     new players[32], inum     get_players(players,inum);     for(new i = 0; i < inum; i++)     {         client_print(i,3,"Test:2");         if(minuteI[i])         {             client_print(i,3,"Test:3");             new authid[32],query[256];             get_user_authid(i,authid,31);             format(query,255,"SELECT balance FROM money WHERE steamid='%s'",authid);             if(mysql_query(mysql,query) == 0)             {                 server_print("Query failed!");                 return PLUGIN_HANDLED;             }             new strBalance[32],strIncome[32];             if(mysql_nextrow(mysql) > 0)             {                 client_print(i,3,"Test:4");                 mysql_getfield(mysql,1,strBalance,31);                 mysql_getfield(mysql,3,strIncome,31);                 new balance = str_to_num(strBalance)                 new inc = str_to_num(strIncome);                 format(query,255,"UPDATE money SET balance = %i WHERE steamid='%s'",balance+inc,authid);                 client_print(i,3,"Balance: %i | Income: %i  | Balance + Income: %i / %s",balance,inc,balance+inc,authid)                 if(mysql_query(mysql,query) == 0)                 {                         server_print("Query failed!");                         return PLUGIN_HANDLED;                 }             }         }     }     return PLUGIN_HANDLED; }

Johnny got his gun 05-31-2004 09:34

Code:
            format(query,255,"SELECT balance FROM money WHERE steamid='%s'",authid);             if(mysql_query(mysql,query) == 0)             {                 server_print("Query failed!");                 return PLUGIN_HANDLED;             }             new strBalance[32],strIncome[32];             if(mysql_nextrow(mysql) > 0)             {                 client_print(i,3,"Test:4");                 mysql_getfield(mysql,1,strBalance,31);                 mysql_getfield(mysql,3,strIncome,31);

SELECT balance FROM money WHERE steamid='%s'"

<< you are only selecting balance. So you only get one field.
You can do like this:
Code:
format(query,255,"SELECT balance FROM money WHERE steamid='%s'",authid); const FIELD_BALANCE = 1 // then later when you want to get the field: mysql_getfield(mysql,FIELD_BALANCE,strBalance,31);
So it's a nice thing to only use defined constants when referring to a field.
If you wanted to get the income field also (which I think you really wanted to do?), you would have to change the select query to something like this:
Code:
format(query,255,"SELECT balance, income FROM money WHERE steamid='%s'",authid); const FIELD_BALANCE = 1 const FIELD_INCOME = 2 // then later when you want to get the fields: mysql_getfield(mysql,FIELD_BALANCE,strBalance,31); mysql_getfield(mysql,FIELD_INCOME,strIncome,31);

I hope you get the picture, you can only get the fields which you select. and they come in the order, 1, 2 and so on... defining constants right after the select has helped me keep the fields in order nice and straight.

Might wanna take a look at syntax for SELECT: http://dev.mysql.com/doc/mysql/en/SELECT.html

BAILOPAN 05-31-2004 13:39

Re: MySQL Prob
 
Just a note for 0.20 DBI will look like this (don't do this now, keep it in mind):

Code:
public pay() {     client_print(0,3,"Test:1");     new players[32], inum     get_players(players,inum);     for(new i = 0; i < inum; i++)     {         client_print(i,3,"Test:2");         if(minuteI[i])         {             client_print(i,3,"Test:3");             new authid[32],query[256];             get_user_authid(i,authid,31);             format(query,255,"SELECT balance FROM money WHERE steamid='%s'",authid);             new res = dbi_query(mysql, query)             if(res < 0)             {                 server_print("Query failed!");                 return PLUGIN_HANDLED;             }             new strBalance[32],strIncome[32];             if(dbi_nextrow(mysql, res) > 0)             {                 client_print(i,3,"Test:4");                 dbi_result(mysql, res, "balance", strBalance, 31)                 dbi_result(mysql, res, "income", strIncome, 31)                 new balance = str_to_num(strBalance)                 new inc = str_to_num(strIncome);                 format(query,255,"UPDATE money SET balance = %i WHERE steamid='%s'",balance+inc,authid);                 client_print(i,3,"Balance: %i | Income: %i  | Balance + Income: %i / %s",balance,inc,balance+inc,authid)                 dbi_free_result(res)                 if(dbi_query(mysql,query) < 0)                 {                         server_print("Query failed!");                         return PLUGIN_HANDLED;                 }             }         }     }     return PLUGIN_HANDLED; }

Relevant changes:
You access fields by name, not by row
mysql_query returns 0 on SUCCESS if it has NO RESULT
mysql_query returns a RESULT HANDLE to access results or a NEGATIVE NUMBER for failure.
You use a RESULT HANDLE to get result specific information - this will let up execute multiple queries in loops.
You must free the result of any query.

This makes it similar to PHP's method, but it breaks a lot of backward compatibility.

Freecode 05-31-2004 14:55

tnx alot jghg and bail.
and i do noe mysql syntax is just i wrote that 4 in the morning and i was way tooooooooo tired to go on.


All times are GMT -4. The time now is 07:02.

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