Category Archives: Programming

GENERATING BATCH NUMBER WITH PHP/MYSQL

Generating batch no most time might look difficult, while surfing the internet I was able to add trick to a style.

Code Sample: 

$connection = mysql_connect(“localhost”,”root”,””);
if(!$connection){
die(“database connection failed: “. mysql_error());
}

$db_select = mysql_select_db(“pin”,$connection);
if(!$db_select){
die(“error selecting from d database”);
}
for ($index = 0; $index < 100; $index++) {
$rand1 = rand(1000000, 9999999); $rand2 = rand(1000000, 9999999);
$pin = $rand1.$rand2;

$check = mysql_query(“SELECT * FROM pin WHERE pin=’$pin’”);
if(mysql_num_rows($check)>0){
$index-=1;
}elseif (mysql_num_rows($check)==0) {
mysql_query(“INSERT INTO pin (pin) values ($pin)”);
$sn = mysql_query(“SELECT serial FROM pin WHERE pin=’$pin’”);
while ($row1 =mysql_fetch_array($sn)) {
$pinSerial = $row1[“serial”];
}
echo”pin: ” .$pin .” serials: “.$pinSerial.”\n”;
}
}

Output: 

table