【问题标题】:How can I retrieve data from MySQL table and insert it into QR code using PHP?如何从 MySQL 表中检索数据并使用 PHP 将其插入 QR 码?
【发布时间】:2014-10-03 08:34:02
【问题描述】:

我有这个 PHP 代码,它用给定的数据创建一个二维码:

<?php

   include(JPATH_LIBRARIES . '/phpqrcode/qrlib.php');

   $tempDir = JPATH_SITE . '/images/';   
   $codeContents = 'This Goes From File';
   $fileName     = 'qr_'.md5($codeContents).'.png';

   $pngAbsoluteFilePath = $tempDir.$fileName;
   $urlRelativeFilePath = JUri::root() .'images/' . $fileName;

   if (!file_exists($pngAbsoluteFilePath)) {
      QRcode::png($codeContents, $pngAbsoluteFilePath);
   }
   else {
      echo "Not working!";
   }

   echo '<img src="'.$urlRelativeFilePath.'" />';

?>

我们需要的是连接到一个 MySQL 表 (#__rsforms_submissions),检索一些字段(例如,姓名和邮件、电话)并将它们插入 QR 码而不是示例提供的数据。为此,我将检索登录用户的用户

$user = JFactory::getUser();
$username = JUserHelper::getUsername($user->id);

然后,MySQL 查询看起来像

SELECT Name,Mail, Phone FROM #__rsforms_submissions WHERE Username = $username

但是,下一步是什么?如何将这些值插入 PHP 代码? 谢谢!

丹妮

【问题讨论】:

    标签: php mysql sql


    【解决方案1】:

    我假设您正在使用 Joomla。我不知道 Joomla,但我可以从 selecting and retrieving SQL data 中弄清楚:

    <?php
    
    /* get username */
    $user = JFactory::getUser();
    $username = JUserHelper::getUsername($user->id);
    
    /* get data from database */
    // Get a db connection.
    $db = JFactory::getDbo();
    
    // Create a new query object.
    $query = $db->getQuery(true);
    
    // Select all records from the user profile table where key begins with "custom.".
    // Order it by the ordering field.
    $query->select($db->quoteName(array('Name', 'Mail', 'Phone')));
    $query->from($db->quoteName('#__rsforms_submissions'));
    $query->where($db->quoteName('Username') . ' LIKE '. $db->quote('\'' . $username . '\''));
    $query->order('ordering ASC');
    
    // Reset the query using our newly populated query object.
    $db->setQuery($query, 0, 1); // 0 = start, 1 = number of records
    $row = $db->loadRow();
    
    /* create the qrcode */
    include(JPATH_LIBRARIES . '/phpqrcode/qrlib.php');
    
    $tempDir = JPATH_SITE . '/images/';
    $codeContents = $row->Name . ', ' . $row->Mail . ', ' . $row->Phone . ', '; // this is what goes into qrcode
    $fileName     = 'qr_'.md5($codeContents).'.png';
    
    $pngAbsoluteFilePath = $tempDir.$fileName;
    
    if (!file_exists($pngAbsoluteFilePath)) {
      QRcode::png($codeContents, $pngAbsoluteFilePath);
    
      $urlRelativeFilePath = JUri::root() .'images/' . $fileName;
      echo '<img src="'.$urlRelativeFilePath.'" />';
    }
    else {
      echo "Not working!";
    }
    

    注意:它未经测试!但是,它应该让您了解如何操作。

    【讨论】:

    • 感谢 machineaddict。我正在运行 Joomla,但我可以使用 Sourcerer 运行原始 PHP。我试试看。
    猜你喜欢
    • 1970-01-01
    • 2019-01-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-25
    • 2013-04-24
    • 2020-06-04
    • 1970-01-01
    相关资源
    最近更新 更多