Iterators problem with mysql
#1

PHP код:
stock save(playerid)
{
    new 
Iteratortest_iter<20>;
    new 
tmp_uidnick[30];
    
mysql_query(MyDB"SELECT uid, Nick FROM players WHERE PD = 1");
    new 
cache_num_rows();
    
    for(new 
nr 0nr jnr++)
    {
        
cache_get_value_int(nr"uid"tmp_uid);
        
cache_get_value(nr"Nick"nick30);
        
                
printf("From sql: %d",tmp_uid);
        
Iter_Add(test_itertmp_uid);
    }
    
    foreach(new 
load test_iter)
    {
        
printf("From iter: %d"load);
    }

Result:
From sql: 1,2,3,4
From iter: 1

Why iter save only first number?
Reply
#2

Just use IterCount instead of foreach.
Reply
#3

But, I want add uid from sql into iterator. ((When I use IterCount result is 1, why only 1 element is save?))
Reply
#4

@ref
Reply
#5

what do you want to accomplish?
Reply
#6

I want add all numbers of uid from query into test_iter
Reply
#7

Quote:
Originally Posted by Oficer
Посмотреть сообщение
I want add all numbers of uid from query into test_iter
You only can add values to the interator to it's size ( new Iterator:Example<32>; )
For example:

Код:
hook OnScriptInit()
{
	new Iterator:Example<32>;
	for(new idx; idx < 32; idx++)
	{
		new index = random(128);
		Iter_Add(Example, index);
	}

	foreach(new id : Example)
		printf("%i", id);
}
Only theese numbers added for me
Код:
11
14
18
23
Sorry for my english
Reply
#8

This example is ok, but when I want add values from database, iterator save only first record. Just like here:

PHP код:
stock test2(playerid)
{
    new 
Iterator Iter_Test<20>;
    
mysql_query(MyDB"SELECT Money from players"); 
    new 
moneyinfo[128];
    for(new 
0cache_num_rows(); != j; ++i
    {
        
cache_get_value_int(i,"Money"money); 
        
Iter_Add(Iter_Testmoney);
        
        
format(infosizeof(info), "Money: %d"money);
        
SCM(playerid0xFFFFFFFFmoney);
    }
    foreach(new 
id Iter_Test)
    {
        
printf("%d"id);
    }
    return 
1;

Same, iterator don't save all records, but only one:

SCM >> 150, 2000, 350
print Iter_test in foreach, return only 150

Reply
#9

m4karow already mentioned it. The size of the iterator matters. If the size in the example is 20, only numbers between 0 and 19 can be added to the iterator and any other will be ignored.

Why do you want to use an iterator and not a simple array? What is the purpose of saving money in an iterator?
Reply
#10

This is only example, I try save data from database into iterator (uid in list of players). In DB I have 3 uid (3,4,5), but iterator saving only first record (3), so size is right. I don't know why saved only 1 uid.
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)