Skip to content

Instantly share code, notes, and snippets.

@rizalrizal
Created April 15, 2021 01:41
Show Gist options
  • Select an option

  • Save rizalrizal/d1d14201c25e3051dce21ce9664be3dd to your computer and use it in GitHub Desktop.

Select an option

Save rizalrizal/d1d14201c25e3051dce21ce9664be3dd to your computer and use it in GitHub Desktop.
Generate Unique Key PHP & MySql
<?php
// Koneksi
$host = "localhost";
$user = "root";
$pass = "";
$db = "db_generate";
$mysqli = new mysqli($host, $user, $pass, $db);
// Generate Key
$ceksama=TRUE;
while($ceksama==TRUE){
$query_generate = $mysqli->query("SELECT LEFT(MD5(NOW()), 8) as kunci");
$data = mysqli_fetch_array($query_generate);
$key = $data['kunci'];
$query_cek = $mysqli->query("SELECT unique_key FROM tbl_key WHERE unique_key='$key'");
$cekdata = mysqli_num_rows($query_cek);
// Cek Jika Key Sudah Ada Di dalam Tabel
if($cekdata > 0){
$ceksama = TRUE;
echo "Key Sudah Ada, Ulang Lagi <br>";
}else{
$ceksama = FALSE;
// insert Key kedalam Tabel
$insert = $mysqli->query("INSERT INTO tbl_key SET unique_key='$key'");
if($insert){
echo "Key Belum Ada, Insert Berhasil dengan key <strong> $key </strong>";
}else{
echo "Insert Gagal, Silahkan Periksa Coding Anda";
}
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment