This article describes how to use thinkphp to connect to the Oracle database and detailed steps, and attach an example. it is very practical. if you need it, you can refer
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.bitsCN.com/softs/161568.html
ThinkPHP: 3.2.3 Official version
: Http://thinkphp1.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.bitsCN.com/softs/63962.html
Development Tool: NetBeans IDE 7.1.2
: Http://www.bitsCN.com/softs/18343.html
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:
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 to submit public function Create_Post () {$ M_User = new UserModel (); $ data ['username'] = $ this-> _ post ('Username '); $ data ['password'] = md5 ($ this-> _ post ('pwd'); if ($ M_User-> Re Ate () {$ 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 (). '[return]');}
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
'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.