Generate random string in php/mysql -
i need generate random string when insert data in mysql. did read uuid or cast(rand) cant find looks can use it.
my data comes app.
i made new row called code , made unik.
i hope can me :)
how tell insert generate random string row code?
// array json response $response = array(); // check required fields if (isset($_post['name']) && isset($_post['nummer']) && isset($_post['description']) && 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 db $db = new db_connect(); mysql_set_charset("utf8"); // mysql inserting new row $result = mysql_query("insert products(name, nummer, description, dato) values('$name', '$nummer', '$description', '$dato')");
ok got far
if (isset($_post['name']) && isset($_post['nummer']) && isset($_post['description']) && 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 db $db = new db_connect(); function generate_random_string($length = 10) { $characters = '0123456789abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz'; $randomstring = ''; ($i = 0; $i < $length; $i++) { $randomstring .= $characters[rand(0, strlen($characters) - 1)]; } return $randomstring; } // mysql inserting new row $result = mysql_query("insert products(name, nummer, description, dato, code) values('$name', '$nummer', '$description', '$dato', '$randomstring')");
but dont in code row?
return $randomstring; $random_str = generate_random_string(10); } // mysql inserting new row $result = mysql_query("insert products(name, nummer, description, dato, code) values('$name', '$nummer', '$description', '$dato', '$random_str')");
you can try code below:
function generate_random_string($length = 10) { $characters = '0123456789abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz'; $randomstring = ''; ($i = 0; $i < $length; $i++) { $randomstring .= $characters[rand(0, strlen($characters) - 1)]; } return $randomstring; } echo generate_random_string(15); // define amount of string length in parametre
source: php random string generator
if use random number random string code below:
$number = range(100000,999999); shuffle($number); $ran_string = $number[0]; echo $ran_string;
Comments
Post a Comment