如何从 MySQL 表中检索数据并将其插入到使用 PHP 的二维码中? [英] How can I retrieve data from MySQL table and insert it into QR code using PHP?

查看:22
本文介绍了如何从 MySQL 表中检索数据并将其插入到使用 PHP 的二维码中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have this PHP code that creates a QR code with the given data:

<?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),检索一些字段(例如,姓名和邮件,电话)并将它们插入二维码而不是示例提供的数据.为此,我将检索登录用户的用户

What we need is to connect to a MySQL table (#__rsforms_submissions), retrieve some fields (for example, Name and Mail, Phone) and insert them in the QR code instead of the data provided by the example. To do so I would retrieve the logged in user's user

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

然后,MySQL 查询看起来像

Then, the MySQL query would look something like

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

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

However, what's next? How do I insert these values into the PHP code? Thanks!

丹妮

推荐答案

我假设您正在使用 Joomla.我不知道 Joomla,但我可以从 选择和检索 SQL 数据 中了解到:>

I'm assuming you are using Joomla. I don't know Joomla, but as I can figure it out from 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!";
}

注意:未经测试!但是,它应该让您知道如何去做.

Note: It's not tested! However, it should give you an idea about how to do it.

这篇关于如何从 MySQL 表中检索数据并将其插入到使用 PHP 的二维码中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆