View Single Post
  #9  
Old 11-26-2011, 06:38 PM
lerxst2112
Demi-God
 
Join Date: Aug 2010
Posts: 1,742
Default

I would do something like this:

Code:
int retval = 0;
if(database.RunQuery(query, MakeAnyLenString(&query,"select `id` from `items` WHERE `classes`= '%i'", class_id), errbuf, &result))
{
	int selected_row = MakeRandomInt(0, mysql_num_rows(result) - 1);
	mysql_data_seek(result, selected_row);
	if(row = mysql_fetch_row(result))
	{
		retval = atoi(row[0]);
	}
	mysql_free_result(result);
}
return retval;
You don't want to return the result where you did, or you will leak memory because the mysql_free_result() call will be skipped.

This will return 0 if there aren't any items that match the query. You will want to check for that in whatever code you use that calls this as an error.

The query you're using is also a bit suspect. I'm not sure what the value of class_id is, but in the best case, assuming the value is (1 << (client->GetClass() - 1)) you will only select items where the class can exclusively equip them. If the value is just what GetClass() returns then it will only return valid items for a warrior or a cleric.

You probably want something like:
Code:
if(database.RunQuery(query, MakeAnyLenString(&query,"select `id` from `items` WHERE `classes` & (1 << %i)", class_id), errbuf, &result))
Where class_id = client->GetClass() - 1.
Reply With Quote