Function not work
#1

Hi, I made a function who search in players data base the phone nubmer, and make a random phone number,
and i made if any account have the random generated number, to repeat the function, but is not work, what is wrong with him?

HTML Code:
pc FindPhone()
{
//	new Cache:Result,Get[2], randomphonenumber = 1000000 + random(9999999);
	new Cache:Result,Get[2],randomphonenumber = 1 + random(2);
	Result = mysql_query(handle,"SELECT * FROM `players`");
	Get[0]= cache_num_rows();
	for(new x;x<Get[0];x++)
	{
		format(strings, sizeof(strings), "SELECT * FROM `players` WHERE `ID` = %d", x);//
    	Result = mysql_query(handle,strings);
    	cache_get_field_content(0, "PhoneNumber", temp), Get[1] = strval(temp);
	    printf("%d",Get[1]);
		if(randomphonenumber==Get[1])
		{
		    FindPhone();
		    return 1;
		}
	}
    printf("final %d %d",randomphonenumber);
   	cache_delete(Result);
	return 1;
}
In my database there are 3 account with numberphone 0 1 and 2, and the function should return 3 but not do it
I put one charcter to be simple to use
Reply
#2

Why do you first get all data in the players table? You only need to get the phone numbers.
In the loop you do a second query, why? You already have the date in the result!

You need to do these steps:
  1. Query the phone numbers from the table.
  2. Generate a random phone number.
  3. Check if an entry in the query result is equal to the generated phone number.
  4. If it is, generate another number and check again, and so on until you have a unique phone number.
  5. Store the new phone number in the database.
Reply
#3

Well I make simple much as I can, that why I ask here, and what you say that make my function
Reply
#4

First of all, the random() function generates a pseudo random number between 0 and max-1. So the only possible outputs for random(2) are 0 and 1. And therefore the "randomphonenumber" can only be 1 or 2.

Furthermore, this is a pretty horrid way to go about querying the database. You only need to check if the randomly generated number exists, which is as simple as querying the "PhoneNumber" column directly (use it in the WHERE component).

And thirdly, if you just want to get a count of something (e.g. number of players) use the count() aggregate function in a query rather than num_rows. A select star carries over ALL the data, which puts an unnecessary strain on the system. This will also get progressively worse as the database grows. A count() on the other hand will always just return one single number.
Reply
#5

Quote:
Originally Posted by Vince
View Post
First of all, the random() function generates a pseudo random number between 0 and max-1. So the only possible outputs for random(2) are 0 and 1. And therefore the "randomphonenumber" can only be 1 or 2.

Furthermore, this is a pretty horrid way to go about querying the database. You only need to check if the randomly generated number exists, which is as simple as querying the "PhoneNumber" column directly (use it in the WHERE component).

And thirdly, if you just want to get a count of something (e.g. number of players) use the count() aggregate function in a query rather than num_rows. A select star carries over ALL the data, which puts an unnecessary strain on the system. This will also get progressively worse as the database grows. A count() on the other hand will always just return one single number.
Valid points. I can't believe I forgot so much of writing the correct queries for the correct use case, after just 1 year of not having to write them. I'll think I should dust of my books again.
Reply
#6

Well my function will do a lot lag, if there are manny users he it gonna be call very many times,there is another simple way?
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)