Example of using the ThinkPHP framework to call mysql database data

Source: Internet
Author: User
Tags datetime mysql database port number


1. Briefly describe what ThinkPHP is.

 

ThinkPHP was born to simplify enterprise-level application development and Agile WEB application development. It was first born in early 2006, and officially renamed ThinkPHP on New Year's Day in 2007, and was released following the Apache2 open-source protocol. ThinkPHP has been adhering to the simple and practical design principles since its birth. It also focuses on ease of use while maintaining outstanding performance and simplified code. With a large number of original functions and features, with the active participation of the community team, we constantly optimize and improve the usability, scalability, and performance.

ThinkPHP is a fast, compatible, and simple lightweight domestic PHP development framework. It was born in early 2006, formerly known as FCS. ThinkPHP was officially renamed as ThinkPHP on New Year's Day in 2007 and follows the release of the Apache2 open-source protocol, it has been transplanted from the Struts structure and improved. It also draws on many excellent foreign frameworks and models and uses the object-oriented development structure and MVC model, it integrates Struts ideas with TagLib, RoR ORM ing, and ActiveRecord mode.

ThinkPHP supports windows, Unix, Liunx, and other server environments. The official version requires PHP5.0 and later versions, and supports MySql, PgSQL, Sqlite, PDO, and other databases. The ThinkPHP framework does not have any special module requirements, the specific running environment of the application system depends on the modules involved in the development.

As an overall development solution, ThinkPHP can meet most application development needs, because it includes the underlying architecture, compatible processing, Base Class Library, database access layer, template engine, Cache mechanism, plug-in mechanism, role authentication, form processing, and other common components, it is also convenient for cross-version, cross-platform, and cross-database migration. Each component is well-designed and well-developed. The application development process only needs to focus on your business logic.

2. ThinkPHP is a framework structure based on the MVC mode. The internal organization files mainly contain these parts.

Model (M): The Model definition is completed by the Model class. The Model class is located in the LibModel directory under the Project Directory.

Controller (C): both the application controller (core controller) and the Action Controller assume the role of the controller. The difference is that the Action controller completes the business process, and the application controller (App class) is responsible for scheduling control. The Action Controller is located in the LibAction directory under the Project Directory.

View (V): the implementation of a template is unrelated to the framework. It is 100% isolated and can be previewed and created independently. The template directory is located in the Tpl directory under the Project Directory.


3. The following is a simple case of database retrieval.

Index. php entry file, file path (App \)

<? Php
 
Define ("APP_DEBUG", true );
 
 
 
Require './ThinkPHP. Php ';
 
?>
 
Config. php configuration file (App \ Conf \)

<? Php
 
Return array (
 
// 'Config maps '=> 'configuration value'
 
// Mysql database connection configuration
 
'Db _ type' => 'mysql', // Set the database TYPE
 
'Db _ host' => 'localhost', // Set the HOST
 
'Db _ name' => 'bbs ', // Set the database NAME
 
'Db _ user' => 'root', // Set the USER name
 
'Db _ pwd' => '', // Set the password
 
'Db _ port' => '123', // Set the PORT number
 
'Db _ prefix' => '', // you can specify the table PREFIX.
 
 
 
'Tmpl _ L_DELIM '=>' <{', // modify the left delimiter
 
'Tmpl _ R_DELIM '=>'}> ', // modify the right delimiter
 
);
 
?>
 
Index.html View (V) layer File (App \ Tpl \ Index \)

<Html>
 
<Head>
 
<Title> MyThinkPHP </title>
 
</Head>
 
<Body>
 
<P> user name: <{$ Uname}>
 
<P> Password: <{$ Upassword}>
 
<P> Registration time: <{$ DateTime}>
 
</Body>
 
</Html>
 
IndexAction. class. php controller (C) layer File (App \ Lib \ Action \)

<? Php
 
// This class is automatically generated by the system for testing purposes only
 
Class IndexAction extends Action {
 
Public function index (){
 
Echo "This is My Frist ThinkPHP !!! ";
 
$ Form = M ('user')-> select ();
 
Dump ($ form );
 
 
 
$ Uid = $ form [1] ['uid'];
 
$ Uname = $ form [1] ['uname'];
 
$ Upassword = $ form [1] ['upassword'];
 
$ DateTime = $ form [1] ['datetime'];
 
 
 
$ This-> assign ('uid', $ uId );
 
$ This-> assign ('uname', $ Uname );
 
$ This-> assign ('upassword', $ Upassword );
 
$ This-> assign ('datetime', $ DateTime );
 
$ This-> display ();
 
   }
 
}
 
UserModel. class. php: Model (M) layer file. The file path is (App \ Lib \ Model \)

<? Php
 
Class UserModel Extends Model {
 
Public function findall (){
 
$ SQL = 'select * FROM user ';
 
Return $ this-> query ($ SQL );
 
}
 
}
 
?>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.