Loading ...
Sorry, an error occurred while loading the content.
 

Re: [phpXperts] How to insert random value in mysql

Expand Messages
  • Joe Forsythe
    You have a logic issue -- you need to set up a loop to process an update for each individual record so you can keep firing the rand() code over and over. So
    Message 1 of 3 , Feb 21, 2013
      You have a logic issue -- you need to set up a loop to process an update for each individual record so you can keep firing the rand() code over and over.

      So you'd do something like this:

      $id_list = 'SELECT recno FROM registry WHERE recno > 1';
      $id_list = mysql_query($id_list);
      for ($i=0; $i < mysql_num_rows($id_list); $i++) {
          $secret_code = rand(100,1000000);
          mysql_query("UPDATE registry set secret_code = '$secret_code' WHERE recno = ".mysql_result($id_list,$i));
      }
      --
      Joe

      On Wed, Feb 20, 2013 at 9:38 PM, Mety <metysoriano@...> wrote:
       

      I have 2000 records already, however I forgot 1 field which secret code.So I add to my table secret code field. This secret code is a random number rand(100,10000). Below is my command

      $secret_code = rand(100,1000000);
      $update ="UPDATE registry set secret_code = '$secret_code' WHERE recno > 1";
      $update1 = mysql_query($update);
      ?>

      However each record has the same secret code once I execute my sql statement. How to take effect my random value.

      Thanks a lot

    Your message has been successfully submitted and would be delivered to recipients shortly.