Retrieve multiple rows from database
#1

I am using BlueG's MySQL Plugin Latest Version.

I am trying to load houses from the database.

Code:

Код:
stock LoadHouses()
{
	new query[256];

	mysql_format(g_SQL, query, sizeof(query), "SELECT * FROM `houses` WHERE `HouseStatus` = 1");
	mysql_tquery(g_SQL, query, "LoadHousesCb", "");
}

public LoadHousesCb()
{
	new rows, fields, string[512]; 
	cache_get_row_count(rows);
	cache_get_field_count(fields);

	if(rows)
	{
		new temphid, tempowner, tempprice, tempinterior, Float:tempx, Float:tempy, Float:tempz, templock, templevel, tempstatus;

		for(new x = 0; x < cache_num_rows(); x++)
		{
			cache_get_value_name_int(x, "HouseID", temphid);
			House[houses][HouseID] = temphid;
			cache_get_value_name(x, "HouseOwner", "tempowner", sizeof(tempowner));
			House[houses][HouseOwner] = tempowner;
			cache_get_value_name_int(x, "HousePrice", tempprice);
			House[houses][HousePrice] = tempprice;
			cache_get_value_name_int(x, "HouseInterior", tempinterior);
			House[houses][HouseInterior] = tempinterior;
			cache_get_value_name_float(x, "HouseEntranceX", tempx);
			House[houses][HouseEntranceX] = tempx;
			cache_get_value_name_float(x, "HouseEntranceY", tempy);
			House[houses][HouseEntranceY] = tempy;
			cache_get_value_name_float(x, "HouseEntranceZ", tempz);
			House[houses][HouseEntranceZ] = tempz;
			cache_get_value_name_int(x, "isLocked", templock);
			House[houses][isLocked] = templock;
			cache_get_value_name_int(x, "HouseLevel", templevel);
			House[houses][HouseLevel] = templevel;
			cache_get_value_name_int(x, "HouseStatus", tempstatus);
			House[houses][HouseStatus] = tempstatus;
		}
		houses++;
	}
	
	printf("Loaded %d houses.", houses);
}
It always shows 'Loaded 1 houses', though there are 5 houses in my database. Is this the correct way to retrieve multiple rows? Please help! I am stuck from three days.
Reply
#2

That.. i will never do something like this when i'll want to load something, but that's the way i will do it, that's the way i already do so here is:

PHP код:
forward LoadHousesCb( );
public 
LoadHousesCb( )
{
    new 
rows;
    
cache_get_row_countrows );
    if( 
rows )
    {
        new 
idhouses;
        while( 
houses rows )
        {
            
cache_get_value_inthouses"ID"id );
            
Houseid ][ HouseID ] = id;
            
cache_get_value_namehouses"HouseOwner"Houseid ][ HouseOwner ], .max_len 48 );
            
cache_get_value_inthouses"HousePrice"Houseid ][ HousePrice ] );
            
cache_get_value_inthouses"HouseInterior"Houseid ][ HouseInterior ] );
            
cache_get_value_inthouses"isLocked"Houseid ][ isLocked ] );
            
cache_get_value_inthouses"HouseLevel"Houseid ][ HouseLevel ] );
            
cache_get_value_inthouses"HouseStatus"Houseid ][ HouseStatus ] );
            
cache_get_value_floathouses"HouseEntranceX"Houseid ][ HouseEntranceX ] );
            
cache_get_value_floathouses"HouseEntranceY"Houseid ][ HouseEntranceY ] );
            
cache_get_value_floathouses"HouseEntranceZ"Houseid ][ HouseEntranceZ ] );
            
houses ++;
        }
        
printf"Loaded %d houses."houses );
    }
    return 
1;

Reply
#3

Quote:
Originally Posted by FaLLenGirL
Посмотреть сообщение
That.. i will never do something like this when i'll want to load something, but that's the way i will do it, that's the way i already do so here is:

PHP код:
forward LoadHousesCb( );
public 
LoadHousesCb( )
{
    new 
rows;
    
cache_get_row_countrows );
    if( 
rows )
    {
        new 
idhouses;
        while( 
houses rows )
        {
            
cache_get_value_inthouses"ID"id );
            
Houseid ][ HouseID ] = id;
            
cache_get_value_namehouses"HouseOwner"Houseid ][ HouseOwner ], .max_len 48 );
            
cache_get_value_inthouses"HousePrice"Houseid ][ HousePrice ] );
            
cache_get_value_inthouses"HouseInterior"Houseid ][ HouseInterior ] );
            
cache_get_value_inthouses"isLocked"Houseid ][ isLocked ] );
            
cache_get_value_inthouses"HouseLevel"Houseid ][ HouseLevel ] );
            
cache_get_value_inthouses"HouseStatus"Houseid ][ HouseStatus ] );
            
cache_get_value_floathouses"HouseEntranceX"Houseid ][ HouseEntranceX ] );
            
cache_get_value_floathouses"HouseEntranceY"Houseid ][ HouseEntranceY ] );
            
cache_get_value_floathouses"HouseEntranceZ"Houseid ][ HouseEntranceZ ] );
            
houses ++;
        }
        
printf"Loaded %d houses."houses );
    }
    return 
1;

This worked but it is skipping HouseID 1, idk why.
Reply
#4

BTW I have posted my full code here: https://sampforum.blast.hk/showthread.php?tid=650641.
Reply
#5

You should access House[] with the index 'houses'. I assume "ID" is auto-incremented in your database, which begins at 1, which means currently House[] will have no data for index 0.

pawn Код:
House[houses][HouseID]=id;

cache_get_value_name(houses, "HouseOwner", House[houses][HouseOwner], .max_Len = 48);
// ...
Reply
#6

Your line "houses++;" is outside of your loop..so of course it's always gonna show 1
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)