为什么phpqrcode在调用QRCode :: png()之前重新运行所有PHP?

时间:2014-08-06 20:02:41

标签: php mysql qr-code

我的问题涉及使用PHP QR码库:http://phpqrcode.sourceforge.net/

当我运行下面的代码时,最后的QRCode :: png()调用重新运行页面上的所有先前代码,以便生成第二个新的随机字符串,并且插入查询运行一秒钟时间。所以我最终得到了表中的两个新行而不是所需的行。

所以显而易见的问题是:如何在不运行第二次插入查询的情况下获取QR码?

// Connect to the database *
require_once 'db_connect.php';

// Function to generate a randomized string *
function F_generateRandomString($length = 4) {
    $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
    $randomString = '';
    for ($i = 0; $i < $length; $i++) {
        $randomString .= $characters[rand(0, strlen($characters) - 1)];
    }
    return $randomString;
}

// do-while loop to make sure we have a randomized *
// string that isn't alrady in the database        *
do {
    $random_string = F_generateRandomString();
    $q_random_check = "SELECT random_string FROM test_table WHERE random_string = '$random_string'";
    $r_random_check = mysql_query($q_random_check) or die (mysql_error());
    $num_random_check = mysql_num_rows($r_random_check);
}
while ($num_random_check);

// Insert data into database *
$q = "INSERT INTO test_table (random_string) VALUES ('$random_string')";
$r_q = mysql_query($q) or die (mysql_error());

// Form the URL for the QR Code *
$url = "https://example.com?$rxc_random";

// Generate the QR Code *
require_once 'phpqrcode/qrlib.php';
QRcode::png($url);

(值得注意的是,我最终会得到QR码。)


更新: 我认为通过执行插入,我找到了更多的“解决方法”而不是解决方案,然后成功使用header("Location: http://example.com?$rxc_random");将QR代码的生成混合到一个单独的.php页面。

0 个答案:

没有答案