The most detailed and simplest thinkphp link database-TripAdvisor blog

Source: Internet
Author: User
Tags mysql in php template vars

Learn to thinkphp set up a small instance, from the Internet to find an article, itself thinkphp is an instance of the official website, but always error, the following this article is also good, is the most basic, from the configuration file to the connection database, then to the production template, read the template, there is a new MySQL database , are more detailed, thinkphp the underlying core file unchanged, if just learning can refer to the study.

Test system: WIN7, XP

Test tool: Zend studio9

Operating platform: WAMP

Database: MySQL

TP Beta version: 3.0 (emphasis: 3.0 Previous versions are very different on configuration)

1. Configure the Portal file

Portal file: Under the same level directory as thinkphp

Name: index.php

index.php

    1. <?php
    2. Define (' Think_path ', './thinkphp/');
    3. Define (' App_path ', './web3.0/');
    4. Define (' App_name ', ' web3.0 ');
    5. Require Think_path. ' Thinkphp.php ';
    6. ?>

Run Result: See Welcome interface ^_^ Hello, welcome to use thinkphp both to configure Ingress success.

2. How to configure database linking

File path: drive letter: \ Server Path \tp3.0web\web3.0\conf

Open the config.php file and modify it to the following:

    1. <?php
    2. Return Array (
    3. //' config item ' = ' config value '
    4. ' Db_type ' = ' mysql ',
    5. ' db_host ' = ' localhost ',
    6. ' Db_name ' = ' MyApp ', //database named MyApp
    7. ' Db_user ' = ' root ',
    8. ' Db_pwd ' = ',
    9. ' Db_port ' = ' 3306 ',
    10. ' Db_prefix ' = ' think_ ',
    11. //Due to the simplest way of linking, the function is abbreviated
    12. );
    13. ?>

The database name is myapp,mysql in the following ways:

<1> Build a database

CREATE DATABASE ' MyApp ';

<2> Build data table (Think_form is both the name of the datasheet)

  1. CREATE TABLE ' think_form ' (
  2. ' ID ' smallint (4) unsigned not NULL auto_increment,
  3. ' title ' varchar (255) not NULL,
  4. ' Content ' varchar (255) is not NULL,
  5. ' Create_time ' int (one) unsigned not NULL,
  6. ' Update_time ' int (one) unsigned not NULL,
  7. ' Status ' tinyint (1) unsigned not NULL,
  8. ' Email ' varchar (not NULL) ,
  9. PRIMARY KEY (' id ')
  10. ) Engine=myisam DEFAULT Charset=utf8;

<3>: Inserting data

INSERT into ' think_form ' (' id ', ' title ', ' content ', ' create_time ', ' update_time ', ' status ', ' email ') VALUES
(1, ' This is test data ', ' dfdf ', 1212724876, 0, 1, ' [email protected] ')

3. Configuring the module file

File path: Automatically generated files after activating the portal file, where the file name is web3.0

EG: drive letter: \ Server Path \tp3.0web\web3.0\lib\action\indexaction.class.php

Open the IndexAction.class.php file and modify it to read as follows:

    1. <?php
    2. This class is automatically generated by the system and is intended for testing purposes only
    3. Class Indexaction extends Action {
    4. Public Function Index ()
    5. {
    6. $form = D (' form ')->findall (); //Recommended not to use the encapsulated database query method, the details will be updated later
    7. Dump ( $form);
    8. //$this->display ();
    9. }
    10. }
    11. ?>
4. Configure database query statement files

File path: drive letter: \ Server Path \tp3.0web\web3.0\lib\model

Create a template file under the file (I don't know if that's the right thing to say) FormModel.class.php

Drive character: \ server path \tp3.0web\web3.0\lib\model\formmodel.class.php

    1. <?php
    2. Class Formmodel extends Model
    3. {
    4. Public function FindAll ()
    5. {
    6. $sql = ' SELECT ' title ' from Think_form ';
    7. return $this->query ( $sql);
    8. }
    9. }
    10. ?>
Summarize:

Create two files: Portal file index.php and database template file FormModel.class.php

Modified two files: config.php and IndexAction.class.php

Finally Http://localhost/TP3.0WEb/index.php/index/index view the results.

Note: The database connection method only describes the method of configuration and implementation, and does not explain the method. Updated later in detail.

Append: Implement the MVC method to extract the database data and display it in the HTML page.

5. Create a data display template (partial template substitution in HTML)

File path: drive letter: \ Server Path \TP3.0WEB\WEB3.0\TPL

Create an index (I-uppercase) folder under the TPL directory, and then build a index.html (i-lowercase) file under the folder

Drive character: \ server path \tp3.0web\web3.0\tpl\index\index.html

Emphasis: In 2.0, you need to set up the default folder in the TPL directory and then set up the Index folder, and then set up the index.html in the Index folder to calculate the configuration is complete
In index.html, add the following:

    1. <meta http-equiv="Content-type" content= "text/html; Charset=utf-8 ">
    2. <title>{$title}</title>
    3. <body>
    4. {$title}
    5. </body>

The contents of {$title} can be seen replaced after running

6. Modify the contents of step 4 without changing the path.

File path: drive letter: \ Server Path \tp3.0web\web3.0\lib\model

Change the contents of the FormModel.class.php template file to read as follows:

  1. <?php
  2. This class is automatically generated by the system and is intended for testing purposes only
  3. Class Indexaction extends Action {
  4. Public Function Index ()
  5. {
  6. //$form = D (' form ')->findall (); Can be used both up and down
  7. $form = M (' form ')->select ();
  8. Dump ( $form);
  9. //$this->assign (' title ', $form [0]["title"]); Both methods are the same
  10. $this->title = $form [0]["title"];
  11. $this->display (); //To be at index. Display content in HTML must open display, and then replace the template with the corresponding name and path
  12. }
  13. }
  14. ?>

At this point, only the TP database operations that can be queried have been completed.

The most detailed and simplest thinkphp link database-TripAdvisor blog

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.