ThinkPHP connection to the Oracle database detailed tutorial, thinkphporacle
I. Operating Environment Construction
System: 64-bit Windows 7 flagship Edition
PHP environment: wampserver2.2e-php5.4.3-httpd2.2.22-mysql5.5.24 32-bit
: Http://www.wampserver.com/en/
ThinkPHP: 3.0 official version
: Http://thinkphp.cn/down.html
Oracle: Orcale_11gR2 32-bit
: Http://www.oracle.com/technetwork/cn/indexes/downloads/index.html
Database Operation tool: PLSQL Developer 32-bit
: Http://www.allroundautomations.com/plsqldev.html
Development Tool: NetBeans IDE 7.1.2
: Http://netbeans.org/downloads/index.html to download a single PHP version
Note: I have repeatedly stressed the "bit" of the software because it is very important. In general, our system is 64-bit, so it is best to use 64-bit software, however, apart from the system, there is a reason to select 32-bit for all, in order to cooperate with the PHP extension of PLSQL Developer and WAMP. Because PLSQL Developer does not have a 64-bit version. Some friends say that they can use a 64-bit Oracle database and install a 32-bit client. I don't want to do this. If I don't like my operation method, I can bypass it. Of course, if you choose to use SQL Developer that comes with Oracle instead of PLSQL Developer, it is your own business to install 64-bit or 32-bit SQL Developer. PHP needs to enable the corresponding extension to connect to the Oracle database. This extension also requires support from the database client, because the php extension also requires the number of digits corresponding to the database client. Complete the process.
II. Environment Configuration
1. I will not talk about the installation of the operating system. Oracle will solve the problem by myself and NetBeans IDE 7.1.2 will also solve it by myself.
2. I will not talk about Wamp installation. I will not start to study again from DOS.
3. WAMP defines the PHP webpage folder in www under the folder where wamp is installed. I installed it on D drive, so it is D: \ WAMP \ www. We will not make other custom modifications for the moment. Start wamp. If the system tray icon is green, start OK.
4. Open localhost and see the following interface, indicating that the Environment configuration is basically OK. Why is it Basic? Because Oracle configuration has not been set yet.
5, open the PHP extension menu, on the green icon, left click-> PHP extension, click the extension of the php-oci8, at this time the WAMP will restart, wait for the restart to change green, it indicates OK.
6. Open the localhost page again. If "4" is displayed, it indicates that PHP currently supports Oracle.
Note: The wamp and oracle clients I currently use are both 32-bit. If one of them is 64-bit, the oci extension cannot be opened, at the same time, the automatic Environment Monitoring page does not display oci8. If PL/SQL is not used, 32-bit Oracle and 32-bit WAMP must be used together, 64-bit Oracle and 64-bit WAMP must be used together, And else should bypass.
Iii. ThinkPHP Configuration
1. decompress the downloaded official version 3.0. Only the ThinkPHP folder is required in the project. This is the core.
2. Use IDE to create a new project. The project folder is the www folder under the Wamp. If you need to customize another folder, you need to modify the apache configuration file. I will not modify it here.
3. Copy the Thinkphp folder to the project folder and create a new php file named index. php.
4. The IDE already displays these files. Open index. php and write the following content:
<?php define('APP_DEBUG', true); require './ThinkPHP/ThinkPHP.php';
5. Open localhost/project name/index. php In the browser. Thinkphp will help you generate related files and folders.
6. perform operations on the configuration file. Find the config. php file in the Conf folder and modify it as follows:
<? Phpreturn array ('db _ type' => 'oracle ', // Database TYPE 'db _ host' => '2017. 168.0.8 ', // server address 'db _ name' => 'orcl', // database NAME 'db _ user' => 'test ', // username 'db _ pwd' => 'test', // password 'db _ port' => '123', // PORT );
The structure of the Oracle database is different from that of the mysql database. Generally, the database name installed by default is orcl. If you use multiple database listeners, you must set them according to the specific listening fields. For example, my local database is Orcl and listens to another Internet database. The listening string is Orcl2. If you need to connect to this Internet database, you need to write the database name orcl2.
7. After the above configuration, you can connect to the oracle database. However, what do you pay attention to in the actual operations of thinkphp? Listen to the next decomposition.
Recently I collected some questions about how THinkPHP connects to the Oracle database. Many of my friends follow the mysql connection method, which causes some methods to be unavailable in Oreale. For example, the findAll and Select methods cannot be used to obtain the required data. The Create and add methods cannot Create or write data to the database.
As a matter of fact, after debugging for several days, I found the problem and successfully used it properly in my own small project exercises. Now I will share my experience with you.
1. I will not talk about the connection of the database and the content of the configuration file, which has been explained above. Here I will only describe my operations based on an example of a data table.
2. The table structure is as follows:
3. This table has three fields: ID primary key, username, And password. Because oracle converts both the table name and field to uppercase, auto-increment of ID primary key is not supported, I only need to use another method to implement this function, for example: ID auto-sequence + trigger to implement ID auto-increment.
4. In ThinkPHP, Action is a controller, Model is a Model, and view is embodied in a template.
First, let's talk about the controller. I only want to introduce how to add and retrieve lists.
Next, let's talk about the model. Here is the main reason for success. Why? ThinkPHP supports field ing, which is perfect for MYSQL. You don't need to write a MODEL, but it doesn't work for ORALCE. When you use M-> add () to add data, the field is filtered out by the $ this-> _ facade () method. The SQL statement generated in this way cannot be executed. It must be incorrect. As a result, the data cannot be added to the database, so the select () method is also used for filtering.
Again, when I perform single-step debugging and the breakpoint is filtered, the new MODEL is used for the filtering method. This MODEL has an array mapped to fields, this filtering method is compared with this field array. If they are inconsistent, it will be filtered out. After debugging, I found that the new MODEL did not add field ing, And the array is empty, of course, it cannot match the added data fields one by one. This is the key to errors.
The Solution Below is actually very simple, according to the basic MVC structure, whether it is PHP, JAVA or. NET has such a structure, so according to strict standards, the MODEL layer code must be written, that is, to map with the database fields. However, many mysql users do not directly write the code in the MODEL. This kind of habit is used in oracle, and a problem occurs.
5. Write my code for the data table above:
My Action is like this: UserAction. class. php. I only add and search for the Controller as an example, so the code is as follows:
1234567891011121314151617181920212223242526272829303132 |
public function index() { header( "Content-Type:text/html; charset=utf-8" ); $M_User = new UserModel(); $User_List = $M_User ->select(); $this ->assign( 'Title' , 'User Management' ); $this ->assign( 'UserList' , $User_List ); $this ->display(); } // Add a user for submission public function Create_Post() { $M_User = new UserModel(); $data [ 'username' ] = $this ->_post( 'username' ); $data [ 'password' ] = md5( $this ->_post( 'pwd' )); if ( $M_User ->create()) { $Query_Result = $M_User ->add( $data ); if (false !== $Query_Result ) { $this ->success( 'User added successfully' ); } else { $this ->error( 'User addition error' ); } } else { header( "Content-Type:text/html; charset=utf-8" ); exit ( $M_User ->getError() . '[<A href = "javascript: history. back ()"> return </a>]' ); } } |
Action explanation:
$ M_User = new UserModel ();
It is best to write this method because it is always written for. NET reasons. Instantiate a specific model. It is strictly required that the User table be operated.
The code for retrieving POST data will not be explained much.
$ M_User-> create ();
This is a ThinkPHP method. It is good and can help you filter out illegal things. We recommend that you use this method.
$ Query_Result = $ M_User-> add ($ data );
This section is about adding data. I am used to specifying the data to be added because this section needs to be instantiated based on $ M_User and filtered out fields. Of course, we only need to do a good job of MODEL code. The following code is not explained. All official documents are available.
My Model is like this: UserModel. class. php
protected $fields = array ( 'id' , 'username' , 'password' ); |
Model explanation: This is the focus. In this way, the new $ M_User ing field array will not be empty, so that it can correspond to POST data, so that the filtering method can be recognized normally without being filtered.
6. After the above operations, the Oracle database operations are completed. Now I can use the methods provided by ThinkPHP to operate data, including paging (limit ), find (), findAll, and so on.