New features of ThinkPHP3.2.3 database settings_PHP tutorial
New features of ThinkPHP3.2.3 database settings
In the previous article, we summarized the new changes in ThinkPHP3.2. In this article, we will take a detailed look at the database. What are the new features? It’s very detailed. Friends who need it can refer to it.
ThinkPHP3.2.3 version database driver is completely rewritten using PDO. The configuration and use are more flexible and powerful than the previous version. Let’s learn how to use it.
First of all, the database configuration information of 3.2.3 has been adjusted. The complete database settings include:
The code is as follows:
/* Database settings */
'DB_TYPE' => '', // Database type
'DB_HOST' => '', // Server address
'DB_NAME' => '', // Database name
'DB_USER' => '', // Username
'DB_PWD' => '', // Password
'DB_PORT' => '', // Port
'DB_PREFIX' => '', // Database table prefix
'DB_PARAMS' => array(), // Database connection parameters
'DB_DEBUG' => TRUE, // Database debugging mode can record SQL logs after it is turned on
'DB_LITE' => false, // Use database Lite mode
'DB_FIELDS_CACHE' => true, // Enable field caching
'DB_CHARSET' => 'utf8', // The default database encoding is utf8
'DB_DEPLOY_TYPE' => 0, // Database deployment mode: 0 centralized (single server), 1 distributed (master-slave server)
'DB_RW_SEPARATE' => false, // Whether database reading and writing are separated, master-slave mode is valid
'DB_MASTER_NUM' => 1, // Number of master servers after read and write separation
'DB_SLAVE_NO' => '', // Specify the slave server serial number
Compared with version 3.2.2, the following setting parameters have been cancelled:
The code is as follows:
'DB_FIELDTYPE_CHECK' // 3.2.3 forces field type detection
'DB_SQL_BUILD_CACHE' // 3.2.3 canceled the SQL creation cache
'DB_SQL_BUILD_QUEUE' // 3.2.3 canceled SQL creation cache
'DB_SQL_BUILD_LENGTH' // 3.2.3 canceled the SQL creation cache
'DB_SQL_LOG' // Replaced by the new DB_DEBUG parameter
'DB_BIND_PARAM' // The new version uses PDO automatic parameter binding, no setting required
New database setting parameters include:
The code is as follows:
'DB_DEBUG' //Used to enable database debugging mode. Once enabled, SQL logs can be recorded
'DB_LITE' // Whether to use database Lite mode to connect. After enabling, only native SQL queries can be used
The debugging mode of the database in version 3.2.2 is bound to the debugging mode of the project (defined by the APP_DEBUG constant). Starting from version 3.2.3, the debugging mode of the database is set independently (set by the DB_DEBUG parameter).
The DB_TYPE parameter is the database type setting. Currently supported drivers include mysql/sqlite/oracle/pgsql/sqlsrv/firebird (other database types require additional drivers). The settings are as follows:
'DB_TYPE'=>'mysql', // No longer supports setting to PDO, and no longer distinguishes between mysql and mysqli
Copy code
Database connection information, mainly including the following parameters:
The code is as follows:
'DB_HOST' => '', // Server address uses IP address
'DB_NAME' => '', // Database name
'DB_USER' => '', // Username
'DB_PWD' => '', // Password
'DB_PORT' => '', // Port If left blank, the default port will be used
'DB_CHARSET' => '', // Database encoding
The above setting parameters will be automatically converted into PDO connection parameters and passed in when instantiating PDO.
The DB_DSN parameter generally does not need to be set. The system's database driver will set it by default. If it needs to be adjusted, please follow the DSN settings of the relevant database connection of PDO.
DB_PARAMS is used to set the connection parameters of the database and will pass in the fourth parameter of PDO instantiation.
The following is a typical database global setting:
The code is as follows:
'DB_TYPE' => 'mysql', // Database type
'DB_HOST' => '192.168.1.10', // Server address
'DB_NAME' => 'thinkphp', // Database name
'DB_USER' => 'root', // Username
'DB_PWD' => '1234', // Password
'DB_PORT' => '3306', // Port
'DB_PREFIX' => 'think_', // Database table prefix
'DB_CHARSET' => 'utf8', // Database encoding
'DB_DEBUG' => TRUE, // Database debugging mode can record SQL logs after it is turned on
If you set a separate database connection information connection attribute in the model class, you can use the following array or string method:
The code is as follows:
//Set database connection information separately in the model
namespace HomeModel;
use ThinkModel;
class UserModel extends Model{
// Define using array
protected $connection = array(
'db_type' => 'mysql',
'db_user' => 'root',
'db_pwd' => '1234',
'db_host' => '192.168.1.10',
'db_port' => '3306',
'db_name' => 'thinkphp',
'db_charset' => 'utf8',
);
}
Note: The database connection setting parameters set in the model adopt the lowercase name of the global configuration.
or defined in string format, the format is:
Database type://username:password@database address:database port/database name#character set
For example:
The code is as follows:
//Set database connection information separately in the model
namespace HomeModel;
use ThinkModel;
class UserModel extends Model{
// Define
using string mode protected $connection = 'mysql://root:1234@192.168.1.10:3306/thinkphp#utf8';
}
can also be set through configuration files, for example:
The code is as follows:
//Database configuration 1
'DB_CONFIG1' => array(
'db_type' => 'mysql',
'db_user' => 'root',
'db_pwd' => '1234',
'db_host' => '192.168.1.10',
'db_port' => '3306',
'db_name' => 'thinkphp',
'db_charset'=> 'utf8',
),
//Database configuration 2
'DB_CONFIG2' => 'mysql://root:1234@192.168.1.10:3306/thinkphp#utf8';
Then define it in the model:
The code is as follows:
//Set database connection information separately in the model
namespace HomeModel;
use ThinkModel;
class UserModel extends Model{
//Call the database configuration 1 in the configuration file
protected $connection = 'DB_CONFIG1';
// or
protected $connection = 'DB_CONFIG2';
}
In addition to specifying the database connection information when defining the model, we can also specify the database connection information when instantiating it. If the M method is used to instantiate the model, different database connection information can also be passed in. For example:
The code is as follows:
$User = M('User','other_','mysql://root:1234@192.168.1.10/demo#utf8');
means instantiating the User model, connecting to the other_user table of the demo database, and the connection information used is configured by the third parameter.
If we have configured DB_CONFIG2 in the project configuration file, we can also use:
$User = M('User','other_','DB_CONFIG2');
The above is the entire content of this article, I hope you all like it.

TooptimizePHPcodeforreducedmemoryusageandexecutiontime,followthesesteps:1)Usereferencesinsteadofcopyinglargedatastructurestoreducememoryconsumption.2)LeveragePHP'sbuilt-infunctionslikearray_mapforfasterexecution.3)Implementcachingmechanisms,suchasAPC

PHPisusedforsendingemailsduetoitsintegrationwithservermailservicesandexternalSMTPproviders,automatingnotificationsandmarketingcampaigns.1)SetupyourPHPenvironmentwithawebserverandPHP,ensuringthemailfunctionisenabled.2)UseabasicscriptwithPHP'smailfunct

The best way to send emails is to use the PHPMailer library. 1) Using the mail() function is simple but unreliable, which may cause emails to enter spam or cannot be delivered. 2) PHPMailer provides better control and reliability, and supports HTML mail, attachments and SMTP authentication. 3) Make sure SMTP settings are configured correctly and encryption (such as STARTTLS or SSL/TLS) is used to enhance security. 4) For large amounts of emails, consider using a mail queue system to optimize performance.

CustomheadersandadvancedfeaturesinPHPemailenhancefunctionalityandreliability.1)Customheadersaddmetadatafortrackingandcategorization.2)HTMLemailsallowformattingandinteractivity.3)AttachmentscanbesentusinglibrarieslikePHPMailer.4)SMTPauthenticationimpr

Sending mail using PHP and SMTP can be achieved through the PHPMailer library. 1) Install and configure PHPMailer, 2) Set SMTP server details, 3) Define the email content, 4) Send emails and handle errors. Use this method to ensure the reliability and security of emails.

ThebestapproachforsendingemailsinPHPisusingthePHPMailerlibraryduetoitsreliability,featurerichness,andeaseofuse.PHPMailersupportsSMTP,providesdetailederrorhandling,allowssendingHTMLandplaintextemails,supportsattachments,andenhancessecurity.Foroptimalu

The reason for using Dependency Injection (DI) is that it promotes loose coupling, testability, and maintainability of the code. 1) Use constructor to inject dependencies, 2) Avoid using service locators, 3) Use dependency injection containers to manage dependencies, 4) Improve testability through injecting dependencies, 5) Avoid over-injection dependencies, 6) Consider the impact of DI on performance.

PHPperformancetuningiscrucialbecauseitenhancesspeedandefficiency,whicharevitalforwebapplications.1)CachingwithAPCureducesdatabaseloadandimprovesresponsetimes.2)Optimizingdatabasequeriesbyselectingnecessarycolumnsandusingindexingspeedsupdataretrieval.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software
