Databases 2 min read

Using ThinkPHP6 Db Facade for MySQL Queries and Executions

This tutorial explains how to use ThinkPHP6's Db facade to perform MySQL SELECT queries with the query method and INSERT/UPDATE operations with the execute method, including required imports and example code snippets.

php Courses
php Courses
php Courses
Using ThinkPHP6 Db Facade for MySQL Queries and Executions

Prerequisite: ThinkPHP6 separates database and model operations into ThinkORM.

To use the Db class, import the facade think\facade\Db and call methods via the static entry point Db::.

1. query method executes MySQL SELECT queries.

<?php
$sql = "SELECT * FROM `shop_goods` where status=1";
$query = Db::query($sql);
print_r($query);
?>

2. execute method performs MySQL INSERT and UPDATE operations.

<?php
$execute = Db::execute("INSERT INTO `shop_goods` VALUES (3, 1)");
print_r($execute);
$execute = Db::execute("UPDATE `shop_goods` set `price`='1100' where `id`=3 ");
print_r($execute);
?>
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

ORMThinkPHP
php Courses
Written by

php Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.