-3

I need to generate more than 100 thousand. But without any duplicate. I want to insert them into my sql table. I want to store them inside a text file. Any idea please?

function generateRandomString2( $length ) {
   $chars = array_merge(range('a', 'z'), range(0, 9));
   shuffle($chars);
   return implode(array_slice($chars, 0, $length));
}
Mahmoud Yhya
  • 31
  • 1
  • 9

2 Answers2

0

Courtesy

you can use openssl_random_pseudo_bytes

bin2hex(openssl_random_pseudo_bytes(5)) The above will give you something like e9d196aa14, for example.

Alternatively, just take the first 10 chars of your existing MD5 string.

Community
  • 1
  • 1
Mubin
  • 4,325
  • 5
  • 33
  • 55
0

I've taken this answer from a different question, you can find here. Credit to Stephen Watkins.

So first thing you need to do is create your random generated string...

function generateRandomString($length = 10) {
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$charactersLength = strlen($characters);
$randomString = '';
for ($i = 0; $i < $length; $i++) {
    $randomString .= $characters[rand(0, $charactersLength - 1)];
}
return $randomString; }

And then a function to write and check the database...

function inputIntoDB(){ $string = generateRandomString(); $db->where('COLUMN NAME',$string)->getOne('TABLENAME'); if (!$db->count > 0){ //insert data into the database. } }

Edit: Just to claify what the above code does (or should do). It generates the 10 character string. It will then query the database to see if that string exists. If there is no result (!$db->count >0), then we can assume it's not a duplicate so we can go ahead and insert the string into our database.

Edit2: Here's the code if you're using MySQLi commands:

$link = mysqli_connect("localhost", "user", "password", "db name");

function inputIntoDB(){
$string = generateRandomString();
$query = "SELECT * FROM table WHERE columnName = $string";
if (!$result = $mysqli->query($link,$query)) {
    $query = "INSERT INTO table (columnName) VALUES ($string)";
    $mysqli->query($link,$query);
} }
Community
  • 1
  • 1
IsThisJavascript
  • 1,726
  • 2
  • 16
  • 25
  • `$db` is undefined in your function - and I'm guessing it's not an instance of a standard PHP class? – HPierce Sep 22 '15 at 15:05
  • 1
    I use [MySqliDb.php](https://github.com/joshcam/PHP-MySQLi-Database-Class/blob/master/MysqliDb.php) to handle all my database requests. I'll make an edit now to include standard PHP mysql querying. – IsThisJavascript Sep 22 '15 at 15:08
  • I think you should look into [how scoping works in PHP](http://php.net/manual/en/language.variables.scope.php). In the first `inputIntoDB()` `$db` is out of scope, in the second `$link` is out of scope. Further, you've awkwardly switched from procedural PHP to object oriented PHP in the MySQLi example (it wouldn't work). – HPierce Sep 22 '15 at 15:46
  • Thanks dear for your help. but do you know how to generate these codes direct to a file with sure no duplicate? – Mahmoud Yhya Sep 22 '15 at 17:17
  • @MahmoudYhya, If you are writing a text file aswell as a database, then you would include a write-to-file procedure after `$query = "INSERT INTO table (columnName) VALUES ($string)";` – IsThisJavascript Sep 23 '15 at 09:46