Here is my question: I am trying to create a random bar code for my application. I want to check that if that code is already in the column, then generate a new number. Check it again. If it's unique, return it to the caller, else generate again.
I am using a recursive function for this purpose. I have added numbers 1,2,3,4 inside my database so every time it runs. It has to show me 5,6,7,8,9 or 10.
Here is my function:
function generate_barcode(){
    $barcode = rand(1,10);
    $bquery = mysql_num_rows(mysql_query("SELECT * FROM stock_item WHERE barcode='$barcode'"));
    if($bquery==1){
        generate_barcode();
    }else{
        return $barcode;    
    }
 }
And I just tested it like this:
 $a = generate_barcode();
 if(isset($a))
 {
   echo $a;
 }
 else
 {
  echo 'Not Set';
 }  
So the problem is that it is sometimes showing me "Not Set", but I want it to always generate a unique number. I am not inserting the data, so it's not a problem that all of the numbers are reserved.
Someone just guide me and let me know what is wrong with the code. I can use other approaches to do that, but I need to know what is wrong with the supplied code.
 
     
     
     
     
     
    