MySQL and randoms car numbers.
#1

Hello, I made my function:
Код:
stock RandomCarNumbers()
{
	new string[64], query[128];
	format(string,sizeof(string),"{000000}%c%c%c-%i%i%i",(65+random(26)),(65+random(26)),(65+random(26)),1+random(9),random(10),random(10));
	MySQLCheck();
	format(query, sizeof(query), "SELECT * FROM 'cars' WHERE numeriai = %s", string);
	mysql_query(query);
	mysql_store_result();
	if(mysql_num_rows() > 0)
	{	
		// what i should write here?
        }
	else
	{
		mysql_free_result();
		return string;
	}
}
That should make random numbers for my car.
But I want that code dont creat the same numbers.
And i dont know what I should write after:
Код:
if(mysql_num_rows() > 0)
Can someone help me?

P.S. sorry for my bad english
Reply
#2

Can someone help?
Reply
#3

Just use recursion
pawn Код:
stock RandomCarNumbers()
{
    new string[64], query[128];
    format(string,sizeof(string),"{000000}%c%c%c-%i%i%i",(65+random(26)),(65+random(26)),(65+random(26)),1+random(9),random(10),random(10));
    MySQLCheck();
    format(query, sizeof(query), "SELECT * FROM 'cars' WHERE numeriai = %s", string);
    mysql_query(query);
    mysql_store_result();
    if(mysql_num_rows() > 0)
    return
        RandomCarNumbers();
    else
    {
        mysql_free_result();
        return string;
    }
}
It will keep calling itself until you have a car number that's unique.
Reply
#4

Thanks for help
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)