BlueG MySQL - cache_get_row_count and cache_get_value_name
#1

I'm trying to learn how to store server data with mysql and I have two problems.

The first one is that when I use cache_get_row_count it always returns 0 even though there's a matching result in the database.
The second problem is that when I use cache_get_name_value I can't seem to store it in an array.

Problematic code:

Код:
public OnPlayerConnect(playerid)
{
	new query[128];
	GetPlayerName(playerid, pInfo[playerid][pUsername], MAX_PLAYER_NAME);
	mysql_format(database, query, sizeof query, "SELECT * FROM `users` WHERE `username` = %e LIMIT 1", pInfo[playerid][pUsername]);
	mysql_tquery(database, query);
	pInfo[playerid][playerCache] = cache_save();
	cache_set_active(pInfo[playerid][playerCache]);
	new rowCount;
	cache_get_row_count(rowCount); // -- HERE IS THE PROBLEM I ASSUME
	if(rowCount > 0) {
		new test[256]; // don't mind this it's for debugging
		cache_get_value_name(0, "password", pInfo[playerid][pPass], 65); // ---- HERE IS THE PROBLEM I ASSUME
		format(test, sizeof test, "password: %s", pInfo[playerid][pPass]);  // don't mind this it's for debugging
		SendClientMessage(playerid, -1, test);  // don't mind this it's for debugging
		ShowPlayerDialog(playerid, DIALOG_LOGIN, DIALOG_STYLE_PASSWORD, "Login", "You are registered, type your password to log in.", "Login", "Exit");
	} else 
	{
		ShowPlayerDialog(playerid, DIALOG_REGISTER, DIALOG_STYLE_PASSWORD, "Register", "Type a password to register.", "Register", "Exit");
	}	

	return 1;
}
Full code:

Код:
#include <a_samp>
#include <a_mysql>

#define DIALOG_LOGIN 0
#define DIALOG_REGISTER 1

#define ms_host			"localhost"
#define ms_user			"myname"
#define ms_password		"mypassword"
#define ms_database		"mydbname"

new MySQL:database;

enum playerData {
	pID,
	pUsername[25],
	pPass[65],
	Cache:playerCache
}

new pInfo[MAX_PLAYERS][playerData];


public OnGameModeInit()
{
	database = mysql_connect(ms_host, ms_user, ms_password, ms_database);
	mysql_tquery(database, "CREATE TABLE IF NOT EXISTS `users` (`id` int(11) NOT NULL AUTO_INCREMENT, `username` varchar(24) NOT NULL, `password` char(65) NOT NULL, PRIMARY KEY (`ID`), UNIQUE KEY `USERNAME` (`USERNAME`))");
	return 1;
}

public OnGameModeExit()
{
	for(new i=0; i<MAX_PLAYERS;i++)
	{
		if(IsPlayerConnected(i))
		{
			Kick(i);
		}
	}
	return 1;
}

public OnPlayerConnect(playerid)
{
	new query[128];
	GetPlayerName(playerid, pInfo[playerid][pUsername], MAX_PLAYER_NAME);
	mysql_format(database, query, sizeof query, "SELECT * FROM `users` WHERE `username` = %e LIMIT 1", pInfo[playerid][pUsername]);
	mysql_tquery(database, query);
	pInfo[playerid][playerCache] = cache_save();
	cache_set_active(pInfo[playerid][playerCache]);
	new rowCount;
	cache_get_row_count(rowCount);
	if(rowCount > 0) {
		new test[256];
		cache_get_value_name(0, "password", pInfo[playerid][pPass], 65);
		format(test, sizeof test, "password: %s", pInfo[playerid][pPass]);
		SendClientMessage(playerid, -1, test);
		ShowPlayerDialog(playerid, DIALOG_LOGIN, DIALOG_STYLE_PASSWORD, "Login", "You are registered, type your password to log in.", "Login", "Exit");
	} else 
	{
		ShowPlayerDialog(playerid, DIALOG_REGISTER, DIALOG_STYLE_PASSWORD, "Register", "Type a password to register.", "Register", "Exit");
	}	

	return 1;
}

public OnPlayerDisconnect(playerid, reason)
{
	return 1;
}

public OnDialogResponse(playerid, dialogid, response, listitem, inputtext[])
{
	if(dialogid == DIALOG_LOGIN)
	{
		if(!response) return Kick(playerid);
		new hashedPassword[65];
		SHA256_PassHash(inputtext, "78sdjs86d2h", hashedPassword, sizeof hashedPassword);
		if(strcmp(hashedPassword, pInfo[playerid][pPass]) == 0)
		{
			SendClientMessage(playerid, -1, "Welcome back!");
		} else {
			SendClientMessage(playerid, -1, "Wrong password, try again.");
			ShowPlayerDialog(playerid, DIALOG_LOGIN, DIALOG_STYLE_PASSWORD, "Login", "You are registered, type your password to log in.", "Login", "Exit");
		}
	} else if(dialogid == DIALOG_REGISTER)
	{
		if(!response) return Kick(playerid);
		new query[256];
		new test[256];
		SHA256_PassHash(inputtext, "78sdjs86d2h", pInfo[playerid][pPass], 65);
		format(test, sizeof test, "name: %s, pass: %s", pInfo[playerid][pUsername], pInfo[playerid][pPass]);
		SendClientMessage(playerid, -1, test);
		mysql_format(database, query, sizeof query, "INSERT INTO `users` (`username`, `password`) VALUES ('%e', '%s')", pInfo[playerid][pUsername], pInfo[playerid][pPass]);
		mysql_tquery(database, query);
		ShowPlayerDialog(playerid, DIALOG_LOGIN, DIALOG_STYLE_PASSWORD, "Login", "You are registered, type your password to log in.", "Login", "Exit");
	}
	return 1;
}

public OnPlayerRequestSpawn(playerid)
{
	SetSpawnInfo(playerid, 0, 240,1484.4651,-1742.1217,13.5469,2.9337,0,0,0,0,0,0);
	SpawnPlayer(playerid);
	return 1;
}
And as a sidenote, can anyone recommend some good blueg mysql r41 tutrials? Because the one tutorial on the forum doesn't explain the most important parts and the documentation on samp wiki is lacking at best.
Reply
#2

Read This!

Just click the link and study that. That'll help you through, good luck
Reply
#3

i need help
Reply
#4

im unable to join to my fav server
Reply
#5

Quote:
Originally Posted by zeest
Посмотреть сообщение
i need help
If your problem is the same then please follow the link if its not, do a little search within the help section and if you didn't find the same problem then please do make a post, with specific detail of the problem and screenshots will be good.
Reply
#6

Quote:
Originally Posted by JesterlJoker
Посмотреть сообщение
Read This!

Just click the link and study that. That'll help you through, good luck
I've already read this a few times as well as the wiki documentation and it didn't help. That's why I asked for other tutorials at the bottom.
But thanks anyway.
Reply
#7

As you can see the documentation is not complete it had already explained itself, as a documentation which means readers needs to think and understand technical ways. anyways...

First lets start of by showing what the logs shows. Not the serverlogs since it does not contain the logs I need to know the error. Show me the plugin logs below the include folder, show me the text contained from errors to warnings and even inside the plugin folder where the mysql complete logging could be found. A print screen or the whole text could be fine.

Oh yeah before you do that. Delete the logs folder then rerun the server, stop then now do the thing above.
Reply
#8

Код:
[20:29:18] [plugins/mysql] cache_save: no active cache
[20:29:18] [plugins/mysql] cache_set_active: invalid cache id '0'
[20:29:18] [plugins/mysql] cache_get_row_count: no active cache
[20:29:18] [plugins/mysql] cache_get_row_count: no active cache
[20:29:18] [plugins/mysql] error #1054 while executing query "SELECT * FROM `users` WHERE `username` = test2 LIMIT 1": Unknown column 'test2' in 'where clause'
Reply
#9

Ahh! I didn't really read into your code but now I see the error
PHP код:
mysql_format(databasequerysizeof query"SELECT * FROM `users` WHERE `username` = %e LIMIT 1"pInfo[playerid][pUsername]);
mysql_tquery(databasequery); // You are not using a callback 
To use this query properly you need to do this

PHP код:
mysql_format(databasequerysizeof query"SELECT * FROM `users` WHERE `username` = %e LIMIT 1"pInfo[playerid][pUsername]);
mysql_tquery(databasequery"OnPlayerCheckAccount""d"playerid); 
Now the callback should look like this
PHP код:
forward OnPlayerCheckAccount(playerid);
public 
OnPlayerCheckAccount(playerid){
    if(
cache_num_rows()){
        
cache_get_value(0"password"PlayerData[playerid][password], MAX_PASS);
        
cache_get_value(0"salt"PlayerData[playerid][salt], MAX_SALT);
        
ShowPlayerDialog(playeridDIALOG_LOGINDIALOG_STYLE_PASSWORD); // you know the rest
    
}else{
        
ShowPlayerDialog(playeridDIALOG_REGISTERDIALOG_STYLE_PASSWORD"Register""Enter passowrd""Submit""");
    }
    return 
1;

I can also teach you mysql_tquery_inline if you wish.
It will not use external callbacks but inline callbacks
Reply
#10

I actually thought I might have to use a callback like in the tutorial, why is that, does mysql_query send the number of rows it got to the callback?

And yes I would really appreciate if you taught me how to do this with inline callbacks, that's how I wanted to do it to begin with.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)