Basing a boolean function on MySQL data
#1

Hello! A few days ago I've decided to start off my own project of creating a gamemode.

I managed to create a login and register system with MySQL and I also wrote a database for the players.

Currently, I'm working on bans and I made a function that checks an IP and returns if the IP is banned or not

So my IP table contains only 'IP' data and inside it there's an IP address.

My problem is that I couldn't figure out how to return 1 or 0 with MySQL.

Refer to this code:

pawn Код:
stock CheckBan(ip[]) // TO DO: SOLVE THIS
{
    new
            szIp2[MAX_PLAYER_NAME],
            szQuery[128];

    mysql_real_escape_string(ip, szIp2, gMySQLConnection);
   
    format(szQuery, sizeof(szQuery), "SELECT COUNT(*) FROM bans WHERE IP = '%s'", ip);
    mysql_query(szQuery, THREAD_BAN_CHECK, -1, gMySQLConnection);
    return ???;
}
How do I check if the IP is banned and return 1 or 0 at the end.

It doesn't help if I check it through the resultid since evetually the function itself needs to return a value.

Anyone got any idea?
Reply
#2

new count = num_rows
free_result
return count
Reply
#3

Quote:
Originally Posted by Djole1337
Посмотреть сообщение
new count = num_rows
free_result
return count
num_rows will always return 1 when using a COUNT instruction in the query..
---
Use fetch_row function instead.
The first row[and the unique row from the SELECT instruction, returned from the fetch_row function] will contain the numbers 0 or 1 from the query you made.
Then, free the result, and return the value.
Reply
#4

Quote:
Originally Posted by Kikito
Посмотреть сообщение
num_rows will always return 1 when using a COUNT instruction in the query..
---
Use fetch_row function instead.
The first row[and the unique row from the SELECT instruction, returned from the fetch_row function] will contain the numbers 0 or 1 from the query you made.
Then, free the result, and return the value.
Could you give me a quick example ?
Reply
#5

Quote:
Originally Posted by ******
Посмотреть сообщение
Given that you have just started, why re-invent the wheel? There are dozens of user systems about - download one, move on, and get quickly to the interesting and unique aspects of your mode instead.
Yeah but that's what I did all the times I wanted to. I want to make one myself.

If it will be possible to ask, Alex, an example of a function that returns a value based on mysql data?
Reply
#6

Quote:
Originally Posted by Partner
Посмотреть сообщение
Could you give me a quick example ?
https://sampwiki.blast.hk/wiki/MySQL/R33#mysql_format
https://sampwiki.blast.hk/wiki/MySQL/R33#mysql_pquery
https://sampwiki.blast.hk/wiki/MySQL/R33#cache_get_row_int

A little example[Not totally sure if this is correct or not, I don't script PAWN for ages]
pawn Код:
forward Query_IPBanned();

stock CheckBan(ban[]) {
    new query[128], ip[16]; // IPV4 max size is 15 chars, so 16chars with the null('\0') in the end, IPV6 is not supported yet on SA-MP
    mysql_real_escape_string(ban, ip);
    mysql_format(connection_handle, query, 128, "SELECT COUNT(*) FROM `bans` WHERE `IP` = '%s'", ip);
    mysql_pquery(connection_handle, query, "Query_IPBanned");
}

public Query_IPBanned() {
    new count = cache_get_row_int(0,0); // get the row 0, field 0, which contains the output of COUNT.
    if(count >= 1) {
         // ip banned
    } else {
         // ip not banned
    }  
}
By the way, change the "COUNT(*)" for "COUNT(<primary key>)"

I hope this helps you out!
Reply
#7

Quote:
Originally Posted by Kikito
Посмотреть сообщение
By the way, change the "COUNT(*)" for "COUNT(<primary key>)"

I hope this helps you out!
That has no effect at all. COUNT(*) simply counts all the rows in the table, while COUNT(key) counts all the rows in the table for which key is not null. And given that the primary key is never null they will just return the same thing.
Reply
#8

Quote:
Originally Posted by Kikito
Посмотреть сообщение
https://sampwiki.blast.hk/wiki/MySQL/R33#mysql_format
https://sampwiki.blast.hk/wiki/MySQL/R33#mysql_pquery
https://sampwiki.blast.hk/wiki/MySQL/R33#cache_get_row_int

A little example[Not totally sure if this is correct or not, I don't script PAWN for ages]
pawn Код:
forward Query_IPBanned();

stock CheckBan(ban[]) {
    new query[128], ip[16]; // IPV4 max size is 15 chars, so 16chars with the null('\0') in the end, IPV6 is not supported yet on SA-MP
    mysql_real_escape_string(ban, ip);
    mysql_format(connection_handle, query, 128, "SELECT COUNT(*) FROM `bans` WHERE `IP` = '%s'", ip);
    mysql_pquery(connection_handle, query, "Query_IPBanned");
}

public Query_IPBanned() {
    new count = cache_get_row_int(0,0); // get the row 0, field 0, which contains the output of COUNT.
    if(count >= 1) {
         // ip banned
    } else {
         // ip not banned
    }  
}
By the way, change the "COUNT(*)" for "COUNT(<primary key>)"

I hope this helps you out!
Perhaps I wasn't clear-..

I need to use checkban in order to check if someone is banned and return the answer.

If adding a ban will require the BanCheck to be false (ban doesn't exist) and make a ban I'll need CheckBan to return a value based on the existance of that IP on the bans table.

But in order to get the result of the check, I will have to process it on the OnQueryFinish callback which will do no good because I need 'CheckBan' to return a value-..

long story short: I need to check if the IP exists on the 'bans' table without referring the action to OnQueryFinish.
Reply
#9

Quote:
Originally Posted by Vince
Посмотреть сообщение
That has no effect at all. COUNT(*) simply counts all the rows in the table, while COUNT(key) counts all the rows in the table for which key is not null. And given that the primary key is never null they will just return the same thing.
It's just a "good programming habit".

@Last post, then use mysql_query to make the query, and cache_get_row_int to retrieve rows data.
https://sampwiki.blast.hk/wiki/MySQL/R33#mysql_query
Make sure you delete the cache after using it.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)