Mysql / sscanf problem -
Jstylezzz - 13.10.2013
Hi all
I've been having some problems with loading mysql data into enums. I'm not sure why, I searched the interwebs, but I couldn't find anything that solved my problem. This is the code I use:
pawn Code:
new Query[1000];
HouseCount = 0;
format(Query,1000,"SELECT * FROM houses WHERE Created = '1'");
mysql_query(Query);
mysql_store_result();
while(mysql_fetch_row(Query))
{
sscanf(Query,"e<p<|>s[32]ddddfffdds[128]s[128]d>",HInfo[HouseCount]);
HouseCount++;
}
I load the houses with the code above. The enum, is as follows:
pawn Code:
enum HouseInfo{
Owner[MPN], //MPN is 32
InteriorID,
Locked,
Owned,
Price,
Float:XOut,
Float:YOut,
Float:ZOut,
Created,
HouseNumber,
StreetName[128],
PhoneNumber[128],
HouseID
}
new HInfo[MAX_HOUSES][HouseInfo];
Now here's the problem, only the ownername is loaded, as you can see in this print debug:
Code:
[12:11:26] ******* LOADING HOUSE 1 ************
[12:11:26] Owner: No-One
[12:11:26] Locked: 0
[12:11:26] Owned: 0
[12:11:26] Price:0
[12:11:26] XOut: 0.000000
[12:11:26] YOut: 0.000000
[12:11:26] ZOut: 0.000000
[12:11:26] Created: 0
[12:11:26] HouseNumber: 1
[12:11:26] *************************************
I'm sure it's the sscanf code though, since the mysql debug is as follows:
Code:
No-One|1|1|0|95000|2067.7|-1628.86|14.5166|1|56|Test.Street|030125187|1
Any help would be greatly appreciated, thanks in advance!
Re: Mysql / sscanf problem -
iZN - 13.10.2013
This same happens to me while loading the houses, I've to restart the server everytime until the houses are properly loaded.
Re: Mysql / sscanf problem -
Jstylezzz - 13.10.2013
I restarted the server a lot of times, but it doesn't change :\
Re: Mysql / sscanf problem - Patrick - 13.10.2013
Try these
pawn Code:
new Query[512];
for(new i; i < MAX_HOUSES; i++) // Goes through all the slots, looking for the data
{
HouseCount = 0;
format(Query,sizeof(Query),"SELECT * FROM houses WHERE Created = %d", i);//searching for the house id and load it
mysql_query(Query);
mysql_store_result();
while(mysql_fetch_row(Query))
{
sscanf(Query,"e<p<|>s[32]ddddfffdds[128]s[128]d>",HInfo[i]); //i = MAX_HOUSES
HouseCount++;
}
}
The problem is this HInfo[HouseCount]
Re: Mysql / sscanf problem -
Jstylezzz - 13.10.2013
Quote:
Originally Posted by pds2012
Try these
pawn Code:
new Query[512]; for(new i; i < MAX_HOUSES; i++) // Goes through all the slots, looking for the data { HouseCount = 0; format(Query,sizeof(Query),"SELECT * FROM houses WHERE Created = %d", i);//searching for the house id and load it mysql_query(Query); mysql_store_result(); while(mysql_fetch_row(Query)) { sscanf(Query,"e<p<|>s[32]ddddfffdds[128]s[128]d>",HInfo[i]); //i = MAX_HOUSES HouseCount++; } }
The problem is this HInfo[HouseCount]
|
Thanks for the reply

I tried it, but now it's just trying to load house 1 like, a hundred times :P. HouseCount has no defined size, and increases each the while loop loops, so I don't think that's the problem.
Re: Mysql / sscanf problem -
Jstylezzz - 14.10.2013
I tried a few things to solve this problem, but I haven't succeeded. I don't like bumping, but I really can't find it myself :\
Re: Mysql / sscanf problem -
Misiur - 14.10.2013
First of all, why aren't you using threaded queries? In the future it will be major pain in the ass. With orm you don't even have to bother with queries! Here's solution for current problem though:
pawn Code:
static const
sql[] = "SELECT * FROM houses WHERE Created = 1";
new
result[384],
i;
mysql_query(sql); //Fetch all houses where created is set to 1
mysql_store_result(); //
while(i != MAX_HOUSES && mysql_fetch_row(result))
{
sscanf(result, "e<p<|>s[32]ddddfffdds[128]s[128]d>", HInfo[i]);
i++;
}
mysql_free_result();
Think about using custom iterator:
pawn Code:
new
HInfo[MAX_HOUSES][HouseInfo],
Iterator:HInfo<MAX_HOUSES>;
//(...)
sscanf(result, "e<p<|>s[32]ddddfffdds[128]s[128]d>", HInfo[i]);
Iter_Add(HInfo, i);
//Then when you have to loop through existing houses
foreach(new house:HInfo) {
printf("House at slot %d is created!", house);
}
Also think about using mysql plugin r34, with orm (tutorial
here)
Re: Mysql / sscanf problem -
Jstylezzz - 14.10.2013
Thanks for the help, I'll take a look into threaded queries