Last active
August 29, 2015 13:55
-
-
Save stuartbates/8730077 to your computer and use it in GitHub Desktop.
Low level PHP making barcodes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
if ( !isset( $_GET['batch'] ) ) | |
die("YOU MUST SPECIFY A BATCH PARAMETER"); | |
$batch = $_GET['batch']; | |
$dbc = @mysqli_connect( DB_HOST, DB_USER, DB_PASSWORD, DB_NAME ) or die(); | |
$batch_size = 100000; | |
function numbers() | |
{ | |
return str_pad ( mt_rand( 0, 9999 ), 4, "0", STR_PAD_LEFT ); | |
} | |
function letters() | |
{ | |
$charset = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'; | |
$count = strlen($charset); | |
$length = 3; | |
$str = ''; | |
while ($length--) { | |
$str .= $charset[mt_rand(0, $count-1)]; | |
} | |
return $str; | |
} | |
function get_random_code($batch) | |
{ | |
return $batch . letters() . numbers(); | |
} | |
$start = time(); | |
$q = "INSERT IGNORE INTO `barcodes` (`code`, `batch`) VALUES (?,?)"; | |
$stmt = mysqli_prepare( $dbc, $q ); | |
mysqli_stmt_bind_param( $stmt, 'ss', $code, $batch ); | |
$i = 0; | |
while( $i < $batch_size ) | |
{ | |
$code = get_random_code($batch); | |
mysqli_stmt_execute( $stmt ); | |
$i += mysqli_stmt_affected_rows( $stmt ); | |
} | |
$elapsed = time() - $start; | |
echo "$elapsed seconds"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment