Thinkphp simple tutorial (2) thinkphp connection to read MySQL database, thinkphpmysql

Source: Internet
Author: User

Thinkphp simple tutorial (2) thinkphp connection to read MySQL database, thinkphpmysql
First, create a project APP on the local server and configure thinkphp according to the steps described in section 1. Here, for the convenience of testing, set the module path to 'regardless of the foreground and background modules '. /APP /'. 1. Create a database myapp and database table think_form; 2. Write the following information in the configuration file APP/Conf/config. php: <? Phpreturn array (// 'config map '=> 'configuration value' 'db _ type' => 'mysql', // Database TYPE 'db _ host' => '2017. 0.0.1 ', // server address 'db _ name' => 'myapp', // database NAME 'db _ user' => 'root ', // username 'db _ pwd' => '', // password 'db _ port' => '123 ', // port 'db _ prefix' => 'think _ ', // database table PREFIX);?> The port is a string that needs to be enclosed by quotation marks. In the past, 3306 was regarded as the int data type, and the result was always unstable. 3. Write in the Controller APP/Lib/IndexAction. class. php: <? Php // This class is automatically generated by the system and is for test purposes only. class IndexAction extends Action {public function index () {$ m = new Model ('form '); $ arr = $ m-> select (); var_dump ($ arr [0] ['title']) ;}}?> Note: The preceding statement is interpreted as: $ m = new Model (Database Table suffix); the size of the first letter of the suffix is not limited. Note that only the database table suffix can be used, but not the complete database table name. Otherwise, Data Reading fails and the data displayed is null. You can also use $ m = M (Database Table suffix); or $ m = D (Database Table suffix). Finally, run http: // 127.0.0.1/APP/index in the browser. php/Index/index. If successful, you can see the data of the Data Field. Note: In the previous connection, define ('app _ debug', true) was not added to the entry file. As a result, the database fails to be read, but suddenly succeeded, it can also be read if it is commented out again. Is this program necessary? Do not understand the situation? Thinkphp is a pain point. 4. the above operations can basically read the database, but for MVC to connect controller C and view V, another process is required: (1) In the Controller APP/Lib/IndexAction. class. add $ this-> assign ('data', $ arr [0] ['title']); $ this-> display (); // This method is required to output the displayed data in view V. (2) create an Index directory in the APP/Tpl directory (because the class name in the controller is IndexAction, this must be Index. If you want to change it to another directory name, you must first change the IndexAction's index.html to another directory name, and then create a new index.html in the indexfile, write the HTML statement and add the {$ data} variable (because $ data is defined and assigned a value through the assign Method in the controller, you can use the assign Method to assign a variable ). Run http: // 127.0.0.1/APP/index. php/Index/index in the browser. If the result is successful, the data of the data field is displayed. In short, thinkphp is a pain point in connecting to the database !! Remember to add define ('app _ debug', true) to prevent troubles.

 

Appendix 1: Four url access methods // highlights! 1. PATHINFO mode -- Focus !!!!!! Http: // domain name/project name/entry file/Module name/method name/key 1/value 1/key 2/value 22. normal Mode http: // domain name/project name/entry file? M = Module name & a = Method Name & Key 1 = value 1 & Key 2 = value 23. REWRITE mode http: // domain name/project name/Module name/method name/key 1/value 1/key 2/value 24. compatibility mode http: // domain name/project name/entry file? S = Module name/method name/key 1/value 1/key 2/value 2

Http: // 127.0.0.1/APP/index. php/Index/index is the first type.
Appendix 2: controller database operation CURD command: add-C Create $ m-> add () Delete-D delete $ m-> Delete () -U Update $ m-> save () query-R Read $ m-> select () 3: Model traversal array method: controller statement: public function index () {$ m = new Model ('form'); // $ m = new Model (Database Table suffix); instantiate Model $ arr = $ m-> select (); // var_dump ($ arr [0] ['title']); // $ name = 'ken'; // $ this-> assign ('data ', $ name); $ this-> assign ('data', $ arr); $ this-> display ();} view V statement:  

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.