samedi 24 décembre 2016

generating random numbers in php

I want to generate a 5 digit random number which is not in table aand apply to registration. I have below code.. Its is generating a random number,but it showing

" Notice: Undefined variable: id in F:\wamp\www\hello\hello.php on line 8"

please do help me

<?php


error_reporting(E_ALL ^ E_DEPRECATED);
// function to generate random ID number
function createID() {
 for ($i = 1; $i <= 5; $i++) {
  $id .= rand(0,9);
 }
 return $id;
}

// MySQL connect info
mysql_connect("localhost", "root", "");
mysql_select_db("college");
$query = mysql_query("SELECT id FROM college");

// puts all known ID numbers into $ids array
while ($result = mysql_fetch_array($query)) {
 $ids[] = $result["id"];
}

// generates random ID number
$id = createID();

// while the new ID number is found in the $ids array, generate a new $id number
while (in_array($id,$ids)) {
 $id = createID();
}

// output ID number
echo $id;

?>




Aucun commentaire:

Enregistrer un commentaire