Home >Backend Development >PHP Tutorial >ThinkPHP Detailed tutorial on connecting to Oracle database, thinkphporacle_PHP tutorial

ThinkPHP Detailed tutorial on connecting to Oracle database, thinkphporacle_PHP tutorial

WBOY
WBOYOriginal
2016-07-12 08:53:55876browse

Detailed tutorial for ThinkPHP to connect to Oracle database, thinkphporacle

1. 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/


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 php-oci8 extension. At this time, the WAMP will restart and wait for it to turn green after restarting. 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:

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



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

<?php

return array(
    'DB_TYPE' => 'Oracle', // 数据库类型
    'DB_HOST' => '192.168.0.8', // 服务器地址
    'DB_NAME' => 'orcl', // 数据库名
    'DB_USER' => 'test', // 用户名
    'DB_PWD' => 'test', // 密码
    'DB_PORT' => '1521', // 端口
);


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, it is possible to connect to the Oracle database, but there is something to pay attention to in the actual operation of thinkphp, which will be decomposed next time.

Recently, we have collected some issues 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 debugged it for a few days based on the previous problem, 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 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. In the controller, I only make examples of adding and searching, so the code is as follows:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 public function index() {         header("Content-Type:text/html; charset=utf-8");           $M_User new UserModel();           $User_List $M_User->select();           $this->assign('Title''用户管理');           $this->assign('UserList'$User_List);           $this->display(); }       //添加用户提交处理 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('用户添加成功');             else<code class="php keyword">else {<code class="php plain">{                 <code class="php spaces">                $this<code class="php variable">$this->error(<code class="php plain">->error('用户添加错误'<code class="php string">'用户添加错误');<code class="php plain">);             <code class="php spaces">            }<code class="php plain">}         <code class="php spaces">        } <code class="php plain">} else<code class="php keyword">else {<code class="php plain">{             <code class="php spaces">            header(<code class="php plain">header("Content-Type:text/html; charset=utf-8""Content-Type:text/html; charset=utf-8");<code class="php plain">);             <code class="php spaces">            exit<code class="php functions">exit(<code class="php plain">($M_User<code class="php variable">$M_User->getError() . <code class="php plain">->getError() . ' [ <a href="javascript:history.back()">返 回</a> ]'<code class="php string">' [ <a href="javascript:history.back()">返 回</a> ]');<code class="php plain">);         <code class="php spaces">        }<code class="php plain">} }<code class="php plain">}

 Action解释:

$M_User=new UserModel();

这个方法最好这么写,因为做.NET的原因,一直都这么写的。针对具体的模型进行实例化,严格规定我就要对User表进行操作了。

获取POST数据的代码就不多解释了。

$M_User->create();

这是ThinkPHP的一个方法,很好,可以帮你过滤掉非法的东西,建议使用。

$Query_Result = $M_User->add($data);

这一段就是数据的添加,我习惯指定要添加的数据,也是因为这一段需要根据$M_User实例化,并过滤字段。当然了,我们只要做好MODEL的代码,就不会有问题。下面的代码就不解释。官方文档都有。

 

我的Model是这样的:UserModel.class.php

protected<table border="0" cellspacing="0" cellpadding="0"> <tbody> <tr> <td class="code"> <code class="php keyword">protected $fields = array(             'id', 'username', 'password'         );
 $fields<p> <code class="php plain">= <br><code class="php keyword">array<br><code class="php plain">(

            <p><code class="php string">'id'

, <code class="php string">'username'<p align="left"><code class="php plain">, <div style="display:none;"><code class="php string">'password'<span id="url" itemprop="url"> <code class="php spaces">        );<span id="indexUrl" itemprop="indexUrl"> </span> <span id="isOriginal" itemprop="isOriginal"> </span> <span id="isBasedOnUrl" itemprop="isBasedOnUrl"> </span>Model解释:这才是重点,这有这样,new出来的$M_User的映射字段数组才不会为空,这样才能和POST的数据进行对应,才会让过滤方法正常识别,不被过滤。<span id="genre" itemprop="genre"></span><span id="description" itemprop="description"> </span>6,经过了以上的操作,针对Oracle的数据库操作就完成了,我现在也可以任意使用ThinkPHP提供的方法来操作数据了,包括分页(limit),find(),findAll等等。

http://www.bkjia.com/PHPjc/1122766.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/1122766.htmlTechArticleThinkPHP 连接Oracle数据库的详细教程,thinkphporacle 一、操作环境搭建 系统:Windows7 旗舰版 64位 PHP环境:wampserver2.2e-php5.4.3-httpd2.2.22-mysql5.5.2...
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
Previous article:<<< operator in PHP, operator in php_PHP tutorialNext article:<<< operator in PHP, operator in php_PHP tutorial

Related articles

See more