With the popularity of QR codes, many industries have begun to apply them to various scenarios, such as logistics tracking, scanning codes for shopping, etc. In web development, we can use PHP to write code to implement the function of querying the database through QR codes. Below I will introduce how to implement this function based on PHP.
1. Generation of QRCode
Before realizing the function of querying the database by QR code, we need to generate the QR code first. Here, I recommend using the PHPQRCode library to generate QR codes. This library is a lightweight PHP QR code generator that supports customizing QR code size, color, margin and other parameters. We can install the library in the following way:
composer require endroid/qrcode
Then, we can generate the QR code. The following is a simple example:
require_once 'vendor/autoload.php'; use Endroid\QrCode\QrCode; $qrcode = new QrCode('https://example.com'); $qrcode->setSize(300); $qrcode->setMargin(10); $qrcode->setWriterByName('png'); $qrcode->setEncoding('UTF-8'); $qrcode->setErrorCorrectionLevel(QcCode::LEVEL_MEDIUM); header('Content-Type: '.$qrcode->getContentType()); echo $qrcode->writeString();
With the above code, we can generate a QR code with a size of 300 and a margin of 10, and output it in png format. We can access a specified URL address by scanning the QR code, and then realize the function of querying the database through the QR code.
2. QR code scanning and decoding
When the user scans the QR code, we need to decode it into a string. Here, we can use the third-party library zxing to scan and decode QR codes. This library is an open source Java library that can recognize various types of codes including barcodes and QR codes. The following is how to install the zxing library:
composer require jongotlin/zxing-php
Next, we can scan and decode the QR code. The following is a simple example:
require_once 'vendor/autoload.php'; use Zxing\QrReader; $qrcodeReader = new QrReader('qrcode.png'); $text = $qrcodeReader->text(); echo $text;
In the above code, we first use the QrReader class to read the content in the QR code image, and then output it as a string. In this way, we can decode the information obtained by the user by scanning the QR code so that we can use it in the next operation.
3. Database query and result display
After we obtain the information obtained by the user scanning the QR code, we can use this information to perform database query to obtain the corresponding data. Here, we assume that our data table stores some product information, including product ID, name, price, etc.
The following is the code for database query through PHP:
$db = new PDO('mysql:host=localhost;dbname=test;charset=utf8', 'root', 'password'); $stmt = $db->prepare('SELECT * FROM products WHERE id = ?'); $stmt->execute([$productId]); $product = $stmt->fetch(PDO::FETCH_ASSOC);
In the above code, we first use the PDO class to establish a connection with the database, and then query the specified ID through the preprocessing statement Product information. Finally, we use the fetch method to obtain the query results.
Through the above methods, we can display the queried product information to users. The following is a simple example:
echo '商品ID:' . $product['id'] . '<br/>'; echo '商品名称:' . $product['name'] . '<br/>'; echo '商品价格:' . $product['price'] . '<br/>';
Through the above code, we can display the queried product information to the user in the form of text. Of course, we can also display this information in a more beautiful way, such as on a web page.
Summary
In this article, we introduced how to use PHP to query the database through QR codes. In fact, in this way, we can implement many interesting functions, such as product query, ticket management, etc. Of course, when we write code, we also need to pay attention to some security issues, such as preventing SQL injection.
The above is the detailed content of How to implement database query function through QR code in PHP. For more information, please follow other related articles on the PHP Chinese website!