title: Generate random string in php/mysql
body: <p>I need to generate a random string when I insert data in my mysql.
  I did read about uuid or cast(rand) but I cant find anything that looks like I can use it.</p>
  
  <p>My data comes from a app.</p>
  
  <p>I made a new row called code and made it unik.</p>
  
  <p>I hope you can help me :)</p>
  
  <p>how do I tell my insert to generate a random string to my row code?</p>
  
  <pre><code>// array for JSON response
  $response = array();
  
   // check for required fields
  if (isset($_POST['name']) &amp;&amp; isset($_POST['nummer']) &amp;&amp; isset($_POST['description']) &amp;&amp; isset($_POST['dato'])) {
  
  $name = $_POST['name'];
  $nummer = $_POST['nummer'];
  $description = $_POST['description'];
  $dato = $_POST['dato'];
  
  // include db connect class
  require_once __DIR__ . '/db_connect.php';
  
  // connecting to db
  $db = new DB_CONNECT();
  
  mysql_set_charset("utf8");
  
  // mysql inserting a new row
  $result = mysql_query("INSERT INTO products(name, nummer, description, dato) VALUES('$name', '$nummer', '$description', '$dato')");
  </code></pre>
  
  <p>Ok this is what I got so far</p>
  
  <pre><code>if (isset($_POST['name']) &amp;&amp; isset($_POST['nummer']) &amp;&amp; isset($_POST['description']) &amp;&amp; isset($_POST['dato'])) {
  
  $name = $_POST['name'];
  $nummer = $_POST['nummer'];
  $description = $_POST['description'];
  $dato = $_POST['dato'];
  // $code = $_POST['code'];
  
  // include db connect class
  require_once __DIR__ . '/db_connect.php';
  
  // connecting to db
  $db = new DB_CONNECT();
  
    function generate_random_string($length = 10) {
      $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
      $randomString = '';
      for ($i = 0; $i &lt; $length; $i++) {
          $randomString .= $characters[rand(0, strlen($characters) - 1)];
      }
      return $randomString;
    }
  
  
  // mysql inserting a new row
  $result = mysql_query("INSERT INTO products(name, nummer, description, dato, code) VALUES('$name', '$nummer', '$description', '$dato', '$randomString')");
  </code></pre>
  
  <p>But I dont get anything in my code row?</p>
  
  <pre><code>    return $randomString;
  
  $random_str = generate_random_string(10);
  
  }
  
  
  // mysql inserting a new row
  $result = mysql_query("INSERT INTO products(name, nummer, description, dato, code) VALUES('$name', '$nummer', '$description', '$dato', '$random_str')");
  </code></pre>