views:

313

answers:

7

Hi I have a function that generates a random combination.

my function looks like this:

  function random_gen($length) {
  $random= "";
  srand((double)microtime()*1000000);
  $char_list = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  $char_list .= "abcdefghijklmnopqrstuvwxyz";
  $char_list .= "1234567890";
  // Add the special characters to $char_list if needed

  for($i = 0; $i < $length; $i++)
  {
    $random .= substr($char_list,(rand()%(strlen($char_list))), 1);
  }
  return $random;
}

$new_url = random_gen(6);

Now i would like to have a while-loop that checks if $new_url already exist in my database...

And then insert the result like this:

mysql_query("INSERT INTO lank (url, code) VALUES ('$url', '$new_url')"); 

I got everything to work except the while-loop. and i just cant figure out how to do it...

A: 
$inserted = false;
while (!$inserted) {
    $new_url = random_gen(6);
    // query("SELECT COUNT(new_url) AS count FROM lank WHERE new_url = '$url'");
    // check count
    if ($count_from_result == 0) {
        mysql_query("INSERT INTO lank (url, code) VALUES ('$url', '$new_url')"); 
        $inserted = true;
    }
}
sberry2A
+1  A: 

You don't need a while loop, just perform a query

mysql_query("SELECT COUNT(*) FROM lank WHERE code = {$new_url}");
Anthony Forloney
yeah, you actually do need a loop. What if the first one exists? What about the second try? Only reliable way to do that is with a while loop.
davethegr8
A: 

It's pretty straight forward:

$sql = "SELECT COUNT(*) as num FROM lank WHERE code='{$new_url}'";
$result = mysql_query($sql);
$row = mysql_fetch_assoc($result);

while($row['num'] > 0) {
    $new_url = random_gen(6);

    $sql = "SELECT COUNT(*) as num FROM lank WHERE code='{$new_url}'";
    $result = mysql_query($sql);
    $row = mysql_fetch_assoc($result);
}
davethegr8
+2  A: 
  • define your code field as UNIQUE in your database
  • generate a code and run an INSERT
  • check with mysql_affected_rows() if the INSERT actually happened or not (i.e. code already present)

saves you a SELECT query

while ( true ) {
    $new_url = random_gen(6);
    mysql_query("INSERT INTO lank (url, code) VALUES ('$url', '$new_url')");
    if ( mysql_affected_rows() )
        break;
}
kemp
... and then loop until you get a confirmed insert.
davethegr8
True, misunderstood the loop request
kemp
+2  A: 

Use this random_generator

function random_gen($length) {
  $characters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';

  $string = '';
  for ($i = 0; $i < $length; $i++) {
    $string .= $characters[rand(0, strlen($characters) - 1];
  }
  return $string;
}
Yada
Or:$characters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'; return substr(str_shuffle($characters), 0, $length);
GZipp
A: 

This should work, without repeating code:

while(true) {
    $new_url = random_gen(6);

    $sql = "SELECT COUNT(*) FROM lank WHERE code='{$new_url}'";
    $result = mysql_query($sql);
    $row = mysql_fetch_row($result);
    if (!$row[0])
        break;
}
Mark
A: 

Use the uniqid() function instead. It will always generate a random result.

If you need more security (i.e.: you don't want adjacent values), simply hash the output: sha1(uniqid()).

mattbasta