我正在使用这个库https://github.com/tecnickcom/tc-lib-barcode构建一个QRCode系统。它工作得很好,但是由于一些奇怪的原因,它只在循环中输出$row["url_link"]的最后一个结果。让我解释一下。
我有3个结果在"qr_codes"表下列url_link的结果(见下文)
https://google.com https://bing.com https://yahoo.com
它将生成所有3个QRCodes到浏览器窗口,但它们都是相同的QRCodes url链接。三个QRCodes均为https://bing.com
我在这里做错了什么?如有任何帮助,不胜感激
这是我的代码:
$servername = "localhost";
$username = "";
$password = "";
$dbname = "";
// Include the library in your project
require ('vendor/autoload.php');
$barcode = new ComTecnickBarcodeBarcode();
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Get the links from the qr_codes tables
$sql = "SELECT url_link FROM qr_codes";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$dir = "qr-code/";
// Directory to store barcode
if (! is_dir($dir)) {
mkdir($dir, 0777, true);
}
// data string to encode
$source = $row["url_link"];
// *** Generate qr code *** //
// set properties
$qrcodeObj = $barcode->getBarcodeObj('QRCODE,H', $source, - 16, - 16, 'black', array(
- 2,
- 2,
- 2,
- 2
))->setBackgroundColor('#FFF');
// generate qrcode
$imageData = $qrcodeObj->getPngData();
$timestamp = time();
//store in the directory
file_put_contents($dir . $timestamp . '.png', $imageData);
//Output image to the browser
echo '<img src="'.$dir . $timestamp.'.png" width="200px" height="200px">';
// *** Generate qr code *** //
}
} else {
echo "0 results";
}
$conn->close();
如果你还没有解决这个问题;你所要做的就是重命名循环中的每个条目:
改变:
echo '<img src="'.$dir . $timestamp.'.png" width="200px" height="200px">
';
:
echo '<img src="'.$dir . $row['id'].'.png" width="200px" height="200px">
';
不同的是你重命名了循环中的每个项,所以没有一个项是相同的。