Home  >  Article  >  Backend Development  >  ThinkPHP Detailed Tutorial on Connecting to Oracle Database [Full]_PHP Tutorial

ThinkPHP Detailed Tutorial on Connecting to Oracle Database [Full]_PHP Tutorial

WBOY
WBOYOriginal
2016-07-21 15:17:51843browse

1. Operating environment construction

System: Windows7 Ultimate 64-bit
PHP environment: wampserver2.2e-php5.4.3-httpd2.2.22-mysql5.5.24 32-bit Version
download address: http://www.wampserver.com/en/


ThinkPHP: 3.0 official version
Download address: http://thinkphp.cn/down.html


Oracle: Orcale_11gR2 32-bit version
Download address: http://www.oracle.com/technetwork/cn/indexes/downloads/index.html


Database operation tool: PLSQL Developer 32-bit
Download address: http://www.allroundautomations.com/plsqldev.html


Development tools: NetBeans IDE 7.1.2
Download address: http://netbeans.org/downloads/index.html Just download the PHP version

Note: I repeatedly emphasize the "bit" of the software here because it is very important. Generally, our system is 64-bit, so it is best to use 64-bit software, but except for the system, all There is a reason for choosing 32-bit, which is to work with PLSQL Developer and WAMP's PHP extensions. Because PLSQL Developer does not have a 64-bit version. Some friends say that you can use a 64-bit Oracle database and install a 32-bit client. I don't want to do this. If you don't like my way of operation, you can avoid it. Of course, if you do not use PLSQL Developer, but choose to use Oracle's own SQL Developer, then it is up to you to install 64-bit or 32-bit. PHP needs to open the corresponding extension to connect to the Oracle database. This extension also requires the support of the database client, because the PHP extension also needs to correspond to the number of bits of the database client. End of verbosity.

2. Environment configuration

1. I will not talk about the installation of the operating system. Oracle installation can be solved by itself, and NetBeans IDE 7.1.2 can also be solved by itself.

2. I won’t talk about the installation of Wamp. If you don’t know how, just start learning again from DOS.

3. WAMP will define the PHP web page folder in www under the folder where wamp is installed. I installed it on the D drive, so it is D:WAMPwww. We will not make other custom modifications for the time being. Start wamp. If the system tray icon is green, it means the startup is OK.

4. Open localhost and see the following interface, which means that the environment configuration is basically OK. Why is it basic? Because the Oracle configuration has not been set yet.

5. Open the PHP extension menu as shown in the picture. On the green icon, left-click ->PHP->PHP extension, click on the extension of php-oci8. At this time, the WAMP will restart and wait until it turns green. It means OK.

6. Open the localhost page again. If you find the display shown in Figure 4, it means that PHP currently supports Oracle.

Note that the wamp and oracle clients I am using are both 32-bit. If one of them is 64-bit, then the oci extension cannot be opened, and there is no oci8 display on the automatic environment monitoring page. Under the premise of not using PL/SQL, it must be a combination of 32-bit Oracle and 32-bit WAMP, or a combination of 64-bit Oracle and 64-bit WAMP. Otherwise, please avoid it.

3. ThinkPHP configuration

1. Unzip the downloaded 3.0 official version. You only need the ThinkPHP folder in the project, which is the core.
2. Use the IDE to create a new project. The project folder is the www folder under Wamp just now. If you need to customize other folders, you need to modify the apache configuration file. I will not modify it here.
3. Copy the Thinkphp folder to the project folder, create a new php file, and name it index.php.
4. These files are already displayed in the IDE. Open index.php and write the following content:

Copy the code The code is as follows:

define('APP_DEBUG', true);
require './ThinkPHP/ThinkPHP.php';

5, in the browser Open localhost/project name/index.php and Thinkphp will generate relevant files and folders for you.
6. Operate the configuration file, find: config.php file in the Conf folder, modify it as follows:
Copy the code The code is as follows:

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' => '1521', // Port
);

The structures of Oracle database and mysql are different. Generally, the database name installed by default is orcl. If you use multiple database monitors, you must set them according to the specific monitor fields. For example: My local database is Orcl, and I am monitoring another database on the external network. The monitoring string is Orcl2. If you need to connect to this external network database, the database name you need to write is orcl2.

7. After the above configuration, you can already connect to the Oracle database, but what should you pay attention to in the actual operation of thinkphp? Let’s take a look below.

Recently, we have collected some questions about THinkPHP connecting to Oracle database. Many friends follow the method of connecting to mysql, resulting in some methods that cannot be used normally in Oreale. For example: findAll, Select methods cannot be used, and the required data cannot be obtained. The Create and add methods cannot create and write data to the database.

In fact, I did a few days of debugging based on previous problems, found the problem, and successfully used it normally in a small project practice of my own, so now I will share my experience with everyone.

1. I won’t go into details about the database connection and configuration file. They have been explained above. I will only illustrate my operation based on an example of a data table.

2, the table structure is as follows:



3. There are 3 fields in this table, ID primary key, username and password. Because Oracle database converts table names and fields to uppercase, and does not support auto-increment of ID primary key, I only have Use another method to implement this function, such as: ID automatic sequence + trigger to implement ID auto-increment.

4. In ThinkPHP, Action is the controller, Model is the model, and the view is represented by a template.

First of all, talking about the controller, I will only introduce the methods of adding and getting the list.

Secondly, talking about the model, this is the main reason for success. Why? ThinkPHP has field mapping, which is perfect for supporting MYSQL. You basically don’t need to write MODEL, but it doesn’t work for ORALCE. When using M->add() to add data, the fields will be $this-> _facade() method filters out. The SQL statement generated in this way cannot be executed and must be wrong. As a result, the data cannot be added to the database, and the select() method will also be filtered.

Again, when I single-step debug and the breakpoints are filtered, the filtering method uses the new MODEL. This MODEL will have an array of field mappings in it. This filtering method is related to this field. Compare the arrays and filter them out if they are inconsistent. As a result, I debugged and found that the new MODEL did not add field mapping at all, and the array was directly empty. Of course, it could not correspond to the added data fields one by one. This is the crux of the mistake.

Let’s talk about the solution below. It’s actually very simple. According to the basic MVC structure, whether it is PHP, JAVA or .NET, all have such structures. Then according to strict standards, the code of the MODEL layer must be written, that is To be mapped to database fields. But many people who use mysql simply do not write the code in MODEL. When this habit is used in Oracle, there is a problem.

5. Next, write my code for the data table above:

My Action is like this: UserAction.class.php. For the controller, I only give examples of adding and searching, so the code is as follows:

Copy code 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 user submission processing
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() . ' [ return ]');
}
}

Action explanation:

$M_User=new UserModel();

This method is best written like this, because it has always been written like this because of .NET. To instantiate a specific model, it is strictly required that I operate on the User table.

The code for obtaining POST data will not be explained much.

$M_User->create();

This is a method of ThinkPHP. It is very good and can help you filter out illegal things. It is recommended to use it.

$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 according to $M_User and filter fields. Of course, as long as we code the MODEL well, there will be no problem. The following code will not be explained. There are official documents.

My Model is like this: UserModel.class.php

protected $fields = array( 'id', 'username', 'password' );

Model explanation: This is the key point. This way, the mapping field array of $M_User generated by new will not be empty. Only in this way can it correspond to the POST data, so that the filtering method can recognize it normally and not be filtered. .

6. After the above operations, the database operation for Oracle is completed. I can now use the methods provided by ThinkPHP to operate data, including paging (limit), find(), findAll, etc.

Source http://www.cnblogs.com/aceliu/

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/325643.htmlTechArticle1. Operating environment setup system: Windows7 Ultimate 64-bit PHP environment: wampserver2.2e-php5.4.3-httpd2 .2.22-mysql5.5.24 32-bit version download address: http://www.wampserver.com/en/ ThinkPH...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn