Chris Tei
Chris Tei

Reputation: 316

Connect MySQL db to QR codes

I am trying to set up a library for books in my work place. I have the MySQL database set up, with the data and everything. Now what i am trying to do is have the ability for someone to take their phone, scan a QR code for a certain book, and it would check out the book. I have a field in the database that states if the book was checked out, and another stating check out date. How would i access my database externally through the internet and QR codes, and have the QR code change data in specific fields of my database, thanks. I used phpMyAdmin to set up the database, and my webhost is iPage.com.

Upvotes: 1

Views: 18828

Answers (2)

Ricson John
Ricson John

Reputation: 31

In general that certainly is possible. Actually every QRCode / barcode stores information. But usually the size of that information is pretty small. Typically only references like URLs or numbers (IDs) are stored and the 'real' data is retrieved from a database using those references. But strictly speaking that is a form of storing information in a QRCode / barcode.

Looking closely we see that the data is stored in the code itself, not inside a database if we are talking about that small amount of data. Certainly that data can be stored inside a database when the code is read, all you need is a trivial piece of software. But that makes little sense in most cases, since the data already is stored. If you want to use a database to combine data from different sources, so if you only want to use codes as a means of transport, then reading the codes is just a replacement for typing the information in with a keyboard. So no magic here, nothing code specific you have to consider.

here is the php script to generate QR directly in db

<?php
class QRGenerator { 

    protected $size; 
    protected $data; 
    protected $encoding; 
    protected $errorCorrectionLevel; 
    protected $marginInRows; 
    protected $debug; 

    public function __construct($data='http://www.tutorial.makersofandroid.com',$size='300',$encoding='UTF-8',$errorCorrectionLevel='L',$marginInRows=4,$debug=false) { 

        $this->data=urlencode($data); 
        $this->size=($size>100 && $size<800)? $size : 300; 
        $this->encoding=($encoding == 'Shift_JIS' || $encoding == 'ISO-8859-1' || $encoding == 'UTF-8') ? $encoding : 'UTF-8'; 
        $this->errorCorrectionLevel=($errorCorrectionLevel == 'L' || $errorCorrectionLevel == 'M' || $errorCorrectionLevel == 'Q' || $errorCorrectionLevel == 'H') ?  $errorCorrectionLevel : 'L';
        $this->marginInRows=($marginInRows>0 && $marginInRows<10) ? $marginInRows:4; 
        $this->debug = ($debug==true)? true:false;     
    }
public function generate(){ 

        $QRLink = "https://chart.googleapis.com/chart?cht=qr&chs=".$this->size."x".$this->size.                            "&chl=" . $this->data .  
                   "&choe=" . $this->encoding . 
                   "&chld=" . $this->errorCorrectionLevel . "|" . $this->marginInRows; 
        if ($this->debug) echo   $QRLink;          
        return $QRLink; 
    }
?>

Upvotes: 0

rekire
rekire

Reputation: 47975

As I already noted in a comment you question is simplar to the question Tranfer QR Code Into a MySql Database. But in your question are more points unclear: So far I did some research some guy from the support of iPage.com told me that php is supported by your webhoster in all packages.

So I will give your some hints how you can implement that with php:

For the database access use prepared statements and PDO. Here is a good tutorial about PDO.

For clean and simple urls for some kind of REST API use mod_rewrite with .htaccess:

Basically you have to check if mod rewrite is enabled. In your case you can skip this, but in general you have to check if you have a AllowOverride All statment in your directory directive.

Just put this code in your .htaccess file:

<IfModule mod_rewrite.c>
  RewriteEngine on
  RewriteBase /
  RewriteRule ^/?searchbook/(.*)$ yourphpfile.php?qr=$1
</IfModule>

So you can access the qr code in yourphpfile.php with this simple variable access:

$_GET['qr']

For the scanning part you can use the ZXing Barcode Scanner app:

If the user has the ZXing installed the brower will open the app automatically. If not the user will get a simple site which gives him or her a hint how to install the app.
You just have to link to the page the resulting value of the qr code will be submitted to the url http://example.com/seachbook/QR-code-content.

http://zxing.appspot.com/scan?ret=http%3A%2F%2Fexample.com%2Fsearchbook%2F%7BCODE%7D%2Fdescription&SCAN_FORMATS=QR_CODE

See also the full explanation on the ZXing documentation.

Upvotes: 1

Related Questions