In our last post you learned the methods of how to generate random numbers/strings in php. In this post you will learn how to verify it from database and run the code until it provides an unique random key which is not available in database.
Click here to read our post for generating random key in php.
For checking we will use while loop. It will check regularly in database when its called and it will check untill it provides an unique numbers/strings.
See the code below
$in_id=substr(str_shuffle("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, 8);
$this->db->where('COLUMN NAME HERE',$in_id);
$result=$this->db->get('TABLE NAME HERE');
You just need to enter column name and table name from which you need to check and compare.
Note : This code is made in codeigniter framework. You can customize it according to your need.
We are done. Use this code and it will provide unique ID every time after checking into your database.