Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New to the CF scene
    Join Date
    Mar 2010
    Posts
    8
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Exclamation Set random number in MySql using PHP?

    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:

    PHP Code:
    <?
    $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'");

    ?>

  • #2
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    Your problem is?

  • #3
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,537
    Thanks
    8
    Thanked 1,092 Times in 1,083 Posts
    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:
    $d1=rand(1,5);
    ..... t2d1='$d1' ......

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

  • Users who have thanked mlseim for this post:

    Scowen (03-23-2010)

  • #4
    New to the CF scene
    Join Date
    Mar 2010
    Posts
    8
    Thanks
    2
    Thanked 0 Times in 0 Posts
    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

  • #5
    Senior Coder
    Join Date
    May 2005
    Posts
    2,137
    Thanks
    96
    Thanked 72 Times in 72 Posts
    Just do a smallint (1)? for the database field.

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

    PHP Code:
    $num rand(1,5);
    $resultc mysql_query("UPDATE `****users` SET `t2start`='1', `t2d1`='$num', `t2d2`='$num', `t2d3`='$num' WHERE username='$user_class->username'"); 
    Rowsdower! has accused me of having mental problems, and the administrator allowed it. What a great forum huh?

  • Users who have thanked masterofollies for this post:

    Scowen (03-23-2010)

  • #6
    New to the CF scene
    Join Date
    Mar 2010
    Posts
    8
    Thanks
    2
    Thanked 0 Times in 0 Posts
    No thats just set everyones t2d1 etc to 0

  • #7
    New to the CF scene
    Join Date
    Mar 2010
    Posts
    8
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Nevermind, it worked, many thanks


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •