PHP QR码生成器与SQL语句

时间:2015-01-30 08:37:02

标签: php mysql qr-code

我想使用MYSQL查询的结果并生成一批QR码,通过PhpQrCode有以下php脚本。我需要的只是显示HTML页面上生成的条形码列表。这是我到目前为止写的:

<?php

include "qrlib.php";
require "conf/config.php";

            $con = mysql_connect(DBSERVER,DBUSER,DBPASS);
            mysql_select_db(DBNAME, $con); 
            $barcodes = mysql_query( "SELECT Description FROM dbo_sensorsandparts ORDER BY ID ASC");



while ($row = mysql_fetch_array($barcodes))
{

echo "<html>";

echo "<img src="; 

QRcode::png ($row['Description']);
echo ">";
}
?> 

查询是正确的,因为我测试了它,但我只得到一个带有某种图像损坏的空白页面。有人可以帮我解决一下我做错了吗?

由于

解决如下:

<?php    
require "conf/config.php";

            $con = mysql_connect(DBSERVER,DBUSER,DBPASS);
            mysql_select_db(DBNAME, $con); 
            $barcodes = mysql_query( "SELECT Description FROM dbo_sensorsandparts ORDER BY ID ASC");



    //set it to writable location, a place for temp generated PNG files
    $PNG_TEMP_DIR = dirname(__FILE__).DIRECTORY_SEPARATOR.'temp'.DIRECTORY_SEPARATOR;

    //html PNG location prefix
    $PNG_WEB_DIR = 'temp/';

    include "qrlib.php";    

    //ofcourse we need rights to create temp dir
    if (!file_exists($PNG_TEMP_DIR))
        mkdir($PNG_TEMP_DIR);


   $filename = $PNG_TEMP_DIR.'label.png';


    while ($row = mysql_fetch_array($barcodes))


{
  $filename = $PNG_TEMP_DIR.'label'.$row['Description'].'.png';

        QRcode::png($row['Description'], $filename); 

        echo '<img src="'.$PNG_WEB_DIR.basename($filename).'" /><hr/>'; 
echo $filename;   

    }    

 ?> 

3 个答案:

答案 0 :(得分:1)

要在html页面中渲染图像,请将返回的QRcode图像保存在某个位置,然后将其指定为<img>标记的 src 属性中的链接。

答案 1 :(得分:1)

如果QRcode::png返回原始图像数据,请使用数据URI显示:

$qr_code = base64_encode(QRcode::png ($row['Description']));

$src = 'data: image/png;base64,'.$qr_code;

echo '<img src="', $src, '">';

答案 2 :(得分:0)

解决如下:

<?php    
    require "conf/config.php";

                $con = mysql_connect(DBSERVER,DBUSER,DBPASS);
                mysql_select_db(DBNAME, $con); 
                $barcodes = mysql_query( "SELECT Description FROM dbo_sensorsandparts ORDER BY ID ASC");



        //set it to writable location, a place for temp generated PNG files
        $PNG_TEMP_DIR = dirname(__FILE__).DIRECTORY_SEPARATOR.'temp'.DIRECTORY_SEPARATOR;

        //html PNG location prefix
        $PNG_WEB_DIR = 'temp/';

        include "qrlib.php";    

        //ofcourse we need rights to create temp dir
        if (!file_exists($PNG_TEMP_DIR))
            mkdir($PNG_TEMP_DIR);


       $filename = $PNG_TEMP_DIR.'label.png';


        while ($row = mysql_fetch_array($barcodes))


    {
      $filename = $PNG_TEMP_DIR.'label'.$row['Description'].'.png';

            QRcode::png($row['Description'], $filename); 

            echo '<img src="'.$PNG_WEB_DIR.basename($filename).'" /><hr/>'; 
    echo $filename;   

        }    

     ?> 

我已经通过变量$ filename将PNG存储为文件。