Home > PHP Framework > ThinkPHP > thinkphp query database returns array

thinkphp query database returns array

WBOY
Release: 2023-05-26 10:05:36
Original
1323 people have browsed it

In the web development process, database query is an inevitable part. Among them, thinkphp, as a PHP framework, provides a wealth of database operation methods. This article discusses how to use thinkphp to query the database and return an array.

1. Environment configuration

Before using thinkphp to perform database operations, you need to perform some environment configuration first. The specific steps are as follows:

  1. Find the database.php file in the root directory of the thinkphp project. This file is the thinkphp database configuration file. Open the file and modify it according to the relevant information of the database. The following fields:
// 数据库类型
'type'        => 'mysql',
// 服务器地址
'hostname'    => 'localhost',
// 数据库名
'database'    => 'database_name',
// 数据库用户名
'username'    => 'root',
// 数据库密码
'password'    => 'root',
// 数据库编码
'charset'     => 'utf8mb4',
// 数据库表前缀
'prefix'      => '',
Copy after login
  1. In the configuration file, we also need to configure the connection information of the database. You can add the following code in config.php:
// 数据库连接参数配置
'db_config'       => [
    // 数据库类型
    'type'        => 'mysql',
    // 服务器地址
    'hostname'    => 'localhost',
    // 数据库名
    'database'    => 'database_name',
    // 数据库用户名
    'username'    => 'root',
    // 数据库密码
    'password'    => 'root',
    // 数据库编码
    'charset'     => 'utf8mb4',
    // 数据库表前缀
    'prefix'      => '',
    // 数据库连接参数
    'params'    => [
        PDO::ATTR_CASE => PDO::CASE_NATURAL, // 不进行大小写转换
        PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, // 抛出异常
        PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, // 默认以关联数组形式返回数据
    ],
],
Copy after login
  1. Add the following code in config.php to enable database configuration and parameters:
// 数据库相关配置
'default_return_type' => 'array', // 默认返回数据集类型为数组

// 数据库配置
'db_config'       => require_once(APP_PATH.'database.php'),
'database'        => $db_config['database'], // 数据库名称
'prefix'          => $db_config['prefix'], // 表前缀
Copy after login
  1. At this point, our environment configuration is complete.

2. Database query operation

If we want to query the database and return an array, we need to use the related methods provided by the Db class encapsulated by thinkphp. The following takes querying the user table as an example.

  1. Query all users
$users = Db::name('user')->select();
dump($users);
Copy after login

In the above code, Db::name('user') means querying the user table, select () means querying all data in the user table and storing the results in the $users variable. dump()The function can output detailed information about variables, making it easier for us to debug the code.

  1. Query a single user
$user = Db::name('user')->where('id', 1)->find();
dump($user);
Copy after login

In the above code, the where() function means querying the user with id 1, find() The function represents querying and returning a piece of data. $userWhat is stored in the variable is the query result.

  1. Query the total number of data
$count = Db::name('user')->count();
echo $count;
Copy after login

In the above code, the count() function can return the total number of data in the user table. We can use echo to output it.

  1. Query user name
$usernames = Db::name('user')->column('name');
dump($usernames);
Copy after login

In the above code, column('name') means that only the name column in the user table is queried, $usernamesWhat is stored in the variable is the query result.

  1. Query user name and age
$userinfos = Db::name('user')->field('name,age')->select();
dump($userinfos);
Copy after login

In the above code, field('name,age') means only querying the name in the user table and age columns, the query results are stored in the $userinfos variable.

  1. Query users who are older than 20 years old
$users = Db::name('user')->where('age', '>', 20)->select();
dump($users);
Copy after login

In the above code, where('age', '>', 20) means To query users whose age is greater than 20, the query results are stored in the $users variable.

  1. Use native SQL statements to query
$users = Db::query('select * from user');
dump($users);
Copy after login

In the above code, Db::query()can use native SQL statements to query the database.

3. Return type of query results

thinkphp supports multiple return types of query results. Here are some common return types.

  1. Array

In the above code, we have learned that thinkphp returns query results of array type by default. You can add the following code in config.php to specify the default return method:

'default_return_type' => 'array',
Copy after login
  1. Object

We can set the query to return the default object type result. Add the following code in config.php:

'default_return_type' => 'object',
Copy after login
  1. JSON

We can set the query results to return json type. Add the following code to config.php:

'default_return_type' => 'json',
Copy after login

IV. Summary

This article mainly introduces how to use thinkphp to query the database and return an array. Among them, we learned about environment configuration, database query operations, return types of query results, etc. In the actual development process, we need to choose the appropriate query method and result return type based on specific project needs. By studying this article, I believe you will have a deeper understanding of thinkphp's database operations.

The above is the detailed content of thinkphp query database returns array. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template