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.
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);
?>Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
php Courses
php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
