Dani Valverde
Dani Valverde

Reputation: 409

How can I create QR code from MySQL data?

I need a script that creates a QR code using user data stored in a MySQL database. It should be loaded every time the user accesses his data. I've done some research and I've found a library that may suit my needs: http://phpqrcode.sourceforge.net/. I've grabbed an example to test it in my website (http://phpqrcode.sourceforge.net/examples/index.php?example=025) and adapted the code:

<?php
include('../libraries/phpqrcode/qrlib.php'); 
include('configuration.php'); 

// how to build raw content - QRCode with simple Business Card (VCard) 

$tempDir = EXAMPLE_TMP_SERVERPATH; 

// here our data 
$name = 'John Doe'; 
$phone = '(049)012-345-678'; 

// we building raw data 
$codeContents = 'BEGIN:VCARD'."\n"; 
$codeContents .= 'FN:'.$name."\n"; 
$codeContents .= 'TEL;WORK;VOICE:'.$phone."\n"; 
$codeContents .= 'END:VCARD'; 

// generating 
QRcode::png($codeContents, $tempDir.'025.png', QR_ECLEVEL_L, 3); 

// displaying 
echo '<img src="'.EXAMPLE_TMP_URLRELPATH.'025.png" />'; 
?>

However, it gives me this error:

Warning: include(../libraries/phpqrcode/qrlib.php): failed to open stream: No such file or directory in /home/u909072349/public_html/plugins/system/sourcerer/helper.php(632) : runtime-created function on line 7 Warning: include(../libraries/phpqrcode/qrlib.php): failed to open stream: No such file or directory in /home/u909072349/public_html/plugins/system/sourcerer/helper.php(632) : runtime-created function on line 7 Warning: include(): Failed opening '../libraries/phpqrcode/qrlib.php' for inclusion (include_path='.:/usr/lib/php') in /home/u909072349/public_html/plugins/system/sourcerer/helper.php(632) : runtime-created function on line 7 Fatal error: Cannot redeclare class JConfig in /home/u909072349/public_html/configuration.php on line 2 I've checked the qrlib.php file and this is the content:

<?php
/*
 * PHP QR Code encoder
 *
 * Root library file, prepares environment and includes dependencies
 *
 * Based on libqrencode C library distributed under LGPL 2.1
 * Copyright (C) 2006, 2007, 2008, 2009 Kentaro Fukuchi <[email protected]>
 *
 * PHP QR Code is distributed under LGPL 3
 * Copyright (C) 2010 Dominik Dzienia <deltalab at poczta dot fm>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
 */

    $QR_BASEDIR = dirname(__FILE__).DIRECTORY_SEPARATOR;

    // Required libs

    include $QR_BASEDIR."qrconst.php";
    include $QR_BASEDIR."qrconfig.php";
    include $QR_BASEDIR."qrtools.php";
    include $QR_BASEDIR."qrspec.php";
    include $QR_BASEDIR."qrimage.php";
    include $QR_BASEDIR."qrinput.php";
    include $QR_BASEDIR."qrbitstream.php";
    include $QR_BASEDIR."qrsplit.php";
    include $QR_BASEDIR."qrrscode.php";
    include $QR_BASEDIR."qrmask.php";
    include $QR_BASEDIR."qrencode.php";

I've checked the libraries and they are all in the same /public_html/libraries/phpqrcode folder. Also, the configuration.php file is in /public_html/. So, can anyone point me the problem? Once solved it I'll move forward. Thanks!

Dani

Upvotes: 2

Views: 6105

Answers (1)

Lodder
Lodder

Reputation: 19743

You haven't defined a base path when including your files.

Try using the following:

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

I'm not sure why you need to include the configuration.php file, however this is not a good idea. If you need to get any values from this file, Joomla has it's own API to achieve this.

Update:

You have not defined you paths correctly. Use the following:

<?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.'" />';

?>

Hope this helps

Upvotes: 1

Related Questions