PDA

View Full Version : Threaded query problem


WhoIsYourDaddy
25/02/2016, 03:13 PM
Hi guys, I have a problem. See, I want to get the house's owner name by searching houseOwner's ID in 'characters' table but just couldn't do it.

Here's my code:

forward evSahip(evID); // testing
public evSahip(evID)
{
new
rows,
fields,
sahipIsim[24];

cache_get_data(rows, fields, g_iHandle);
cache_get_field_content(rows, "Character", sahipIsim, g_iHandle, sizeof(sahipIsim));
strpack(HouseData[evID][houseOwner], sahipIsim, 24 char);

return 1;
}

stock evSahipIsim(evID)
{
static
query[128],
evSahipID;

evSahipID = HouseData[evID][houseOwner];


format(query, sizeof(query), "SELECT `Character` FROM `characters` WHERE `ID` = '%d'", evSahipID);
mysql_tquery(g_iHandle, query, "evSahip", "d", evID);

return 1;
}


It always returns a null value. Where is the problem?

WhoIsYourDaddy
25/02/2016, 03:44 PM
Guys please, it's urgent.

WhoIsYourDaddy
25/02/2016, 07:57 PM
Bump

AmigaBlizzard
25/02/2016, 08:34 PM
cache_get_data(rows, fields, g_iHandle);

This returns the amount of rows and fields. If you have 1 row returned, rows will be 1.


cache_get_field_content(rows, "Character", sahipIsim, g_iHandle, sizeof(sahipIsim));

In case of 1 row returned, you use "rows" here to indicate the row to be processed.
But the first row in a result-set is row 0.
You're processing a non-existing row.


cache_get_field_content(rows - 1, "Character", sahipIsim, g_iHandle, sizeof(sahipIsim));


Or better yet, if you're certain you only have 1 character to be returned (always 1 row):

cache_get_field_content(0, "Character", sahipIsim, g_iHandle, sizeof(sahipIsim));

WhoIsYourDaddy
26/02/2016, 12:30 AM
cache_get_data(rows, fields, g_iHandle);

This returns the amount of rows and fields. If you have 1 row returned, rows will be 1.


cache_get_field_content(rows, "Character", sahipIsim, g_iHandle, sizeof(sahipIsim));

In case of 1 row returned, you use "rows" here to indicate the row to be processed.
But the first row in a result-set is row 0.
You're processing a non-existing row.


cache_get_field_content(rows - 1, "Character", sahipIsim, g_iHandle, sizeof(sahipIsim));


Or better yet, if you're certain you only have 1 character to be returned (always 1 row):

cache_get_field_content(0, "Character", sahipIsim, g_iHandle, sizeof(sahipIsim));

I see. But can you help me with getting the owner's name? It's south central mode. I just couldn't do it as you saw.

Jefff
26/02/2016, 12:35 AM
1. evSahipID = HouseData[evID][houseOwner]; its integer array?
2. cache_get_field_content(rows, "Character", sahipIsim, g_iHandle, sizeof(sahipIsim)); all what you need is replace 'rows' to 0
3. strpack(HouseData[evID][houseOwner], sahipIsim, 24 char); so its string not integer?

WhoIsYourDaddy
26/02/2016, 01:09 AM
1. evSahipID = HouseData[evID][houseOwner]; its integer array?
2. cache_get_field_content(rows, "Character", sahipIsim, g_iHandle, sizeof(sahipIsim)); all what you need is replace 'rows' to 0
3. strpack(HouseData[evID][houseOwner], sahipIsim, 24 char); so its string not integer?
1. Yes, houseOwner is an integer. It keeps house owner's ID.
2. I don't know what I did there. It is a result of hours of desperate work, sadly...
3. I tried to find user's ID in 'houses' table and search it in 'characters' and find the owner's name. Didn't quite do it.

So here's the deal. House's and business's owners are saved by their SQLID not by name. And I want to get the owner's name using these IDs. It's South Central gamemode if you want to look at it. Obviously I can't do it by myself.

Virtual1ty
26/02/2016, 01:01 PM
@OP
AmigaBlizzard and Jefff have already pointed you where your problem is.
The evSahipIsim() part is correct, your evSahip() "callback" is not.


forward evSahip(evID);
public evSahip(evID)
{
new
rows,
fields,
sahipIsim[24];

cache_get_data(rows, fields, g_iHandle);
cache_get_field_content(0, "Character", sahipIsim, g_iHandle, sizeof(sahipIsim));

// strpack(HouseData[evID][houseOwner], sahipIsim, 24 char);
// ^ you cannot magically store a string to an integer variable

// do what you need with the house owner
// stored as a string in "sahipIsim" array

return 1;
}

WhoIsYourDaddy
26/02/2016, 01:09 PM
@OP
AmigaBlizzard and Jefff have already pointed you where your problem is.
The evSahipIsim() part is correct, your evSahip() "callback" is not.


forward evSahip(evID);
public evSahip(evID)
{
new
rows,
fields,
sahipIsim[24];

cache_get_data(rows, fields, g_iHandle);
cache_get_field_content(0, "Character", sahipIsim, g_iHandle, sizeof(sahipIsim));

// strpack(HouseData[evID][houseOwner], sahipIsim, 24 char);
// ^ you cannot magically store a string to an integer variable

// do what you need with the house owner
// stored as a string in "sahipIsim" array

return 1;
}

Should the row for cache_get_field_content be 0? And can I return the string to get the name in different part of script.

Virtual1ty
26/02/2016, 01:14 PM
Yes it should be as row index starts with 0.
No, you cannot return anything with threaded queries. Instead, you must restructure your code.

However, there's this neat little thing called "inline queries" with y_inline: link (http://forum.sa-mp.com/showthread.php?t=548986).

WhoIsYourDaddy
26/02/2016, 01:18 PM
Yes it should be as row index starts with 0.
No, you cannot return anything with threaded queries. Instead, you must restructure your code.
And how do I do that? I need the evSahip(houseID) function to give me the name of the owner. Okay, script is getting the name but returns 1 instead of owner name.

Virtual1ty
26/02/2016, 01:26 PM
Then simply paste your code to evSahip() function and do what you need to do with sahipIsim variable.
Re-read my previous reply, I have edited it.

WhoIsYourDaddy
26/02/2016, 01:41 PM
Then simply paste your code to evSahip() function and do what you need to do with sahipIsim variable.
Re-read my previous reply, I have edited it.
What if I use the normal mysql_query and then script my function as evSahip(), not as public evSahip() or stock evSahip()?

Virtual1ty
26/02/2016, 01:47 PM
That's your choice, I don't know if it's possible to do that (outside OnGameModeInit) with the latest MySQL plugin.
And yes, you should declare your functions which you intend to use without the stock modifier.

You have been given the explanation, you just need to go and read that topic on how to use y_inline and you're all set.

WhoIsYourDaddy
26/02/2016, 01:56 PM
That's your choice, I don't know if it's possible to do that (outside OnGameModeInit) with the latest MySQL plugin.
And yes, you should declare your functions which you intend to use without the stock modifier.

You have been given the explanation, you just need to go and read that topic on how to use y_inline and you're all set.
Thanks for your help mate. I'll look into it.

WhoIsYourDaddy
26/02/2016, 03:55 PM
evSahip(evID)
{
new
rows,
fields,
sahipIsim[24];

new
query[128],
evSahipID;

evSahipID = HouseData[evID][houseOwner];

cache_get_data(rows, fields, g_iHandle);

mysql_format(g_iHandle, query, sizeof(query),"SELECT `Character` FROM `characters` WHERE `ID` = '%d'", evSahipID);
mysql_tquery(g_iHandle, query, "", "d", evSahipID);

cache_get_field_content(0, "Character", sahipIsim, g_iHandle, sizeof(sahipIsim));

return sahipIsim;
}


This is the last version of my code. When I start my server it creates a mysql_log.txt and in that there is an error saying "CMySQLResult::GetRowDataByName - field not found ("Character")"

N0FeaR
26/02/2016, 03:57 PM
what exactly is the problem here?

WhoIsYourDaddy
26/02/2016, 04:00 PM
what exactly is the problem here?
It creates a mysql log and in there, there is this: CMySQLResult::GetRowDataByName - field not found ("Character")

N0FeaR
26/02/2016, 04:15 PM
It creates a mysql log and in there, there is this: CMySQLResult::GetRowDataByName - field not found ("Character")

I reply to u when i get home cus i am at my phone right now so can't do it now!

Virtual1ty
26/02/2016, 04:26 PM
Same here, on my phone...
You can't use threaded queries like that, as I have already told you. The only possible way is you either restructure your code or use y_inline to do it in the same function, for which I have linked you to the tutorial.

And you still haven't shown us how you intend to use that function. Show us an example of that code and we will be able to further assist you.

WhoIsYourDaddy
26/02/2016, 04:35 PM
Same here, on my phone...
You can't use threaded queries like that, as I have already told you. The only possible way is you either restructure your code or use y_inline to do it in the same function, for which I have linked you to the tutorial.

And you still haven't shown us how you intend to use that function. Show us an example of that code and we will be able to further assist you.
Okay let me try one more...