#1
  1. Full Access
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Jun 2000
    Location
    London, UK
    Posts
    2,019
    Rep Power
    17
    OK, I've tried adding something to a database project I'm working on whereby a random number gets generated by the PHP and added to the database. I stripped out this piece of code which is causing the trouble and made it into a program in its own rights. But it doesn't seem to work. I created a new table called test with one column which is an INT. So the idea is that every time I call this program a random number gets added to the table. But it doesn't work: I get a parse error on the last line of the program, which just contains ?>

    Someone help!

    <?

    MYSQL_CONNECT(localhost,username,password) OR DIE("Unable to connect to database");

    @mysql_select_db("database") or die("Unable to select database");

    srand (time());
    $iNum = rand(100000000, 999999999);


    $query = "INSERT INTO test VALUES($iNum)";
    MYSQL_QUERY($query);
    print "<b>$iNum</b> has been added. Click <a href=id.php3>here</a> to run the ****er again.;

    MYSQL_CLOSE();

    ?>
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2000
    Location
    Ontario, Canada
    Posts
    498
    Rep Power
    18
    You're seeding the random number wrong. Try this (ripped right from the PHP manual no less :P)

    // seed with microseconds since last "whole" second
    srand((double)microtime()*1000000);
    $randval = rand();


    ------------------
    To alcohol! The cause of, and solution to, all of life's problems. -- Homer Simpson
  4. #3
  5. No Profile Picture
    Apprentice Deity
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 1999
    Location
    Niagara Falls (On the wrong side of the gorge)
    Posts
    3,237
    Rep Power
    19
    and... the reason you are getting a parse error on the last line is that you don't have a closing quote on your print statement.
  6. #4
  7. Full Access
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Jun 2000
    Location
    London, UK
    Posts
    2,019
    Rep Power
    17
    Thanks once again, I got one of my web developer friends onto it and he pointed me in the right direction: in the end I realised that the problem lay in the fact that I got the form variables mixed up with the mySQL column names. It's now fixed and working!

    Thanks. TOPIC CLOSED
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 1999
    Posts
    37
    Rep Power
    26
    What's wrong with srand (time()) ? Is it just that it only changes once a second?

Similar Threads

  1. php script won't work inside "cgi-local" directory
    By Volitics in forum PHP Development
    Replies: 5
    Last Post: April 26th, 2004, 11:50 PM
  2. My number one hated thing about msn
    By pojo_65 in forum Dev Shed Lounge
    Replies: 11
    Last Post: February 16th, 2004, 08:40 PM
  3. Assembly Language number input and output.
    By tavenger5 in forum Dev Shed Lounge
    Replies: 5
    Last Post: February 15th, 2004, 11:51 PM
  4. Eliminating duplicate words in random sentences
    By guiness4all in forum C Programming
    Replies: 6
    Last Post: February 9th, 2004, 05:54 PM
  5. random number script
    By sander1971 in forum ASP Programming
    Replies: 4
    Last Post: January 27th, 2004, 10:48 AM

IMN logo majestic logo threadwatch logo seochat tools logo