insertign a random number

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
hame22
Forum Contributor
Posts: 214
Joined: Wed May 11, 2005 5:50 am

insertign a random number

Post by hame22 »

Hi I need some help in inserting a random number in a query

I need a number to be generated between 1 - 20 and then inserted into my database via a query.

Form should be something like this

Code: Select all

$result = mysql_query("Insert into activities set downloads = *random number*");
is there a way to do this either via mysql or php?

thanks in advance
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

In PHP you can use rand(). MySQL also has a RAND() function which works slightly differently.

Mac
bdlang
Forum Contributor
Posts: 395
Joined: Tue May 16, 2006 8:46 pm
Location: Ventura, CA US

Post by bdlang »

PHP Manual: rand()
PHP Manual: mt_rand()

You might find this recent (yesterday) topic useful:
viewtopic.php?t=49523
User avatar
ok
Forum Contributor
Posts: 393
Joined: Wed May 31, 2006 9:20 am
Location: The Holy Land

Post by ok »

Code: Select all

mt_rand(1, 20);
!!!!!
Post Reply