View Full Version : Set random number in MySql using PHP?

03-23-2010, 01:01 AM
Ok so what i want is for when the user clicks on the button, a random number between 1 and 5 (not excluding 1 and 5) will be inserted into my table, here is what ive got so far:

$resultc = mysql_query("UPDATE `****users` SET `t2start`='1', `t2d1`='rand(1,5)', `t2d2`='rand(1,5)', `t2d3`='rand(1,5)' WHERE username='$user_class->username'");


03-23-2010, 01:07 AM
Your problem is?

03-23-2010, 01:07 AM
Probably several ways to do this ...
Is it OK if t2d1, t2d2 and t2d3 have the same numbers in them?
Example, it might be: 1 2 1

Not sure what will happen with this: t2d1`='rand(1,5)'
Maybe you need to do this:
..... t2d1='$d1' ......

With your method, it might insert "rand(1,5)" literally, not as a value.

03-23-2010, 01:13 AM
The problem is that it wont insert this data into the database.

Ive got the fields set as

ENUM "0","1","2","3","4","5"

Does that change anything at all?
Ive tried your method before and it doesnt work unfortuneatly

03-23-2010, 01:16 AM
Just do a smallint (1)? for the database field.

what mlseim said is correct and will work. It goes like this

$num = rand(1,5);
$resultc = mysql_query("UPDATE `****users` SET `t2start`='1', `t2d1`='$num', `t2d2`='$num', `t2d3`='$num' WHERE username='$user_class->username'");

03-23-2010, 01:30 AM
No thats just set everyones t2d1 etc to 0

03-23-2010, 01:40 AM
Nevermind, it worked, many thanks :)