MySQL - Anyone spare a minute?
#1

Right, i would like to grab the following:

'ip' from 'users'.

So it would be

SELECT 'ip' FROM 'users' WHERE 'username' = %s. Yep? Got that.

Now, i have done the query.


How do i store this? I can't seem to figure it out, i want to store it in:

new OBanIP[25]; - This is at the top of the command.

+rep will be given, and i hope you can help me!

Thanks!
Reply
#2

Anyone?
Reply
#3

You could try this:
pawn Code:
mysql_get_field(OBanIP, "ip");
Reply
#4

Quote:
Originally Posted by BlackBank3
View Post
You could try this:
pawn Code:
mysql_get_field(OBanIP, "ip");
Hey, i have already tried that, and i got this error:

Code:
[19:03:37] CMySQLHandler::FetchField(ip) - You cannot call this function now. (Reason: Fields/Rows are empty.)
Do you have MSN? And you could assist me with TeamViewer (if you dont mind! ).


Thanks for the reply.
Reply
#5

EDIT: My fault i see the query already.
Reply
#6

pawn Code:
new query[70];
GetPlayerName(playerid, query, sizeof(query));
mysql_format(1, query, "SELECT ip FROM users WHERE username = '%e';", query);
mysql_query(query, 1/*resultid*/, playerid, 1);
//^ Put that in the function/callback where you want to call the query

public OnQueryFinish( query[], resultid, extraid, connectionHandle )
{
        if(resultid == 1)
        {
                mysql_store_result();
                mysql_retrieve_row();
                mysql_get_field("ip", OBanIP);
                //Whatever you want to do with the IP... Here "extraid" = playerid
                mysql_free_result(); //DON'T forget this! Or you will get "commands out of sync" error
        }
}
If you have multiple threaded queries you will most likely need to change the resultid to an available one

Also, check this: https://sampwiki.blast.hk/wiki/MySQL for documentation of the plugin's natives and some useful examples
Reply
#7

Threads aren't required, but you can't, in SQL, select fields by using a single apostrophe, you need to use a grave (`) or none at all.

Also, make sure you use mysql_store_result() after querying, then mysql_retrieve_row() to get the specific row.
Reply
#8

Quote:
Originally Posted by GamingTurf
View Post
Hey, i have already tried that, and i got this error:

Code:
[19:03:37] CMySQLHandler::FetchField(ip) - You cannot call this function now. (Reason: Fields/Rows are empty.)
Are you sure everything is saving?

Quote:
Originally Posted by Calgon
View Post
Threads aren't required
No, but recommended. I use 'em all the time now.
Reply
#9

Wow, thanks for all the help guys - /oban now works fine.

Butt, i've now tried making /omute, i thought i wouldn't have any problems - i was wrong:

pawn Code:
format(checkquery, sizeof(checkquery), "SELECT `muted` FROM `users` WHERE `username` = '%s'", OBanName);
                    mysql_query(checkquery);
                    mysql_store_result();
                    mysql_retrieve_row();
                    mysql_get_field("muted", IsMutedOrNot); --- Error line ----
                    mysql_free_result();
Error:
Code:
error 035: argument type mismatch (argument 1)
Thanks, if your able to help!


I have, new IsMutedOrNot;

:S.
Reply
#10

Which MySQL plugin are you using?
Reply
#11

G-Stylezz, i think.
Reply
#12

Quote:
Originally Posted by GamingTurf
View Post
Wow, thanks for all the help guys - /oban now works fine.

Butt, i've now tried making /omute, i thought i wouldn't have any problems - i was wrong:

Error:
Code:
error 035: argument type mismatch (argument 1)
Thanks, if your able to help!


I have, new IsMutedOrNot;

:S.
mysql_get_field probably is expecting a string as the second parameter.

pawn Code:
new szUnload[10], IsMutedOrNot;

                    format(checkquery, sizeof(checkquery), "SELECT `muted` FROM `users` WHERE `username` = '%s'", OBanName);
                    mysql_query(checkquery);
                    mysql_store_result();
                    mysql_retrieve_row();
                    mysql_get_field("muted", szUnload); --- Error line ----
                    mysql_free_result();
                 
                                    IsMutedOrNot = strval(szUnload);
Also, as Vince said, you can use mysql_fetch_int which would be more relevant.
Reply
#13

If you're only fetching a single item from the database at a time - be it a string, integer or float - it is recommended to use mysql_fetch_row, mysql_fetch_int and mysql_fetch_float respectively.
Reply
#14

Quote:
Originally Posted by Calgon
View Post
mysql_get_field probably is expecting a string as the second parameter.

pawn Code:
new szUnload[10], IsMutedOrNot;

                    format(checkquery, sizeof(checkquery), "SELECT `muted` FROM `users` WHERE `username` = '%s'", OBanName);
                    mysql_query(checkquery);
                    mysql_store_result();
                    mysql_retrieve_row();
                    mysql_get_field("muted", szUnload); --- Error line ----
                    mysql_free_result();
                 
                                    IsMutedOrNot = strval(szUnload);
Also, as Vince said, you can use mysql_fetch_int which would be more relevant.
Thank you alot!

Thanks to everyone who replied too! .
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)