ThinkPHP detailed tutorial on connecting to the Oracle database [all]

Source: Internet
Author: User
Tags netbeans

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://

ThinkPHP: 3.0 official version
: Http://

Oracle: Orcale_11gR2 32-bit
: Http://

Database Operation tool: PLSQL Developer 32-bit
: Http://

Development Tool: NetBeans IDE 7.1.2
: Http:// 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:

Copy codeThe Code is as follows: <? Php
Define ('app _ debug', true );
Require './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:Copy codeThe Code is as follows: <? Php
Return array (
'Db _ type' => 'oracle ', // Database TYPE
'Db _ host' => '192. 168.0.8 ', // server address
'Db _ name' => 'orcl ', // Database NAME
'Db _ user' => 'test', // USER Name
'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. But what do you need to know about thinkphp's actual operations.

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:

Copy codeThe Code is as follows: 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 added 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.


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: 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.