Table of Contents
ThinkPHP connects to Oracle database, thinkphporacle
Home Backend Development PHP Tutorial ThinkPHP connects to Oracle database, thinkphporacle_PHP tutorial

ThinkPHP connects to Oracle database, thinkphporacle_PHP tutorial

Jul 12, 2016 am 08:53 AM
oracle thinkphp

ThinkPHP connects 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.bkjia.com/softs/161568.html

ThinkPHP: 3.2.3 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.bkjia.com/softs/63962.html

Development tools: NetBeans IDE 7.1.2
Download address: http://www.bkjia.com/softs/18343.html

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 won’t talk about the installation of the operating system. Oracle installation can be solved by yourself, and NetBeans IDE 7.1.2 can also be solved by yourself.

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

<&#63;php
 define('APP_DEBUG', true);
 require './ThinkPHP/ThinkPHP.php';
Copy after login

5. Open localhost/project name/index.php in the browser, and 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:

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

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. However, there are some things 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.

其实根据以前问题我做了几天调试,找到了问题所在,并成功在我自己一个小项目练习中使用正常,那么现在就将我的经验分享给大家。

1,数据库的连接及配置文件的内容我就不说了, 上面已经做了解释。我这里只根据一个数据表的例子来说明我的操作。

2,表结构如下:

3,这个表中有3个字段,ID主键,用户名username和密码password,因为oracle数据库把表名和字段都是转成大写的,同时不支持ID主键自增,我只有使用另外的方法来实现这个功能,比如:ID自动序列+触发器实现ID自增。

4, ThinkPHP中,Action是控制器,Model是模型,视图是以模板方式体现的。

首先,说控制器,我只做增加和获取列表的方法介绍。

其次,说模型,这里才是成功的主要原因。为什么?ThinkPHP是有字段映射的,这个在对MYSQL的支持非常完美,基本不用写MODEL,但是对ORALCE就不行了,当使用M->add()来添加数据时,字段会被$this->_facade()方法过滤掉。这样生成的SQL语句就是没法执行的,肯定是错误的,导致数据添加不到数据库中,那么使用select()方法也是一样被过滤。

再次,当我单步调试时,断点被过滤的时候,过滤方法使用到了new出来的MODEL,这个MODEL会有一个字段映射的数组在里面,这个过滤方法就是和这个字段数组进行对比,如果不一致就过滤掉,结果我调试发现,new出来的MODEL根本没有把字段映射加进去,数组直接为空,当然就没法和添加的数据字段一一对应了。这就是错误的关键。

下面就来说解决方法,其实很简单,按照基本的MVC结构,不管是PHP还是JAVA还是.NET都有这样的结构,那么按照严格的标准,MODEL层的代码是必须写的,就是要和数据库的字段做映射。但是很多用mysql的,就直接没有去写MODEL里面的代码。这种习惯被用到了oracle中,就出了问题。

5, 下面针对我上面的数据表写出我的代码:

我的Action是这样的:UserAction.class.php。控制器我只对添加和查找做例子,因此代码如下:

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 {
    $this->error('用户添加错误');
   }
  } else {
   header("Content-Type:text/html; charset=utf-8");
   exit($M_User->getError() . ' [ <a href="javascript:history.back()">返 回</a> ]');
  }
}
Copy after login

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

'id', 'username', 'password');

Model解释:这才是重点,这有这样,new出来的$M_User的映射字段数组才不会为空,这样才能和POST的数据进行对应,才会让过滤方法正常识别,不被过滤。

6,经过了以上的操作,针对Oracle的数据库操作就完成了,我现在也可以任意使用ThinkPHP提供的方法来操作数据了,包括分页(limit),find(),findAll等等。

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/1122892.htmlTechArticleThinkPHP连接Oracle数据库,thinkphporacle 一、操作环境搭建 系统:Windows7 旗舰版 64位 PHP环境:wampserver2.2e-php5.4.3-httpd2.2.22-mysql5.5.24 32位版 下载地...
Statement of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What to do if the oracle can't be opened What to do if the oracle can't be opened Apr 11, 2025 pm 10:06 PM

Solutions to Oracle cannot be opened include: 1. Start the database service; 2. Start the listener; 3. Check port conflicts; 4. Set environment variables correctly; 5. Make sure the firewall or antivirus software does not block the connection; 6. Check whether the server is closed; 7. Use RMAN to recover corrupt files; 8. Check whether the TNS service name is correct; 9. Check network connection; 10. Reinstall Oracle software.

How to solve the problem of closing oracle cursor How to solve the problem of closing oracle cursor Apr 11, 2025 pm 10:18 PM

The method to solve the Oracle cursor closure problem includes: explicitly closing the cursor using the CLOSE statement. Declare the cursor in the FOR UPDATE clause so that it automatically closes after the scope is ended. Declare the cursor in the USING clause so that it automatically closes when the associated PL/SQL variable is closed. Use exception handling to ensure that the cursor is closed in any exception situation. Use the connection pool to automatically close the cursor. Disable automatic submission and delay cursor closing.

How to create cursors in oracle loop How to create cursors in oracle loop Apr 12, 2025 am 06:18 AM

In Oracle, the FOR LOOP loop can create cursors dynamically. The steps are: 1. Define the cursor type; 2. Create the loop; 3. Create the cursor dynamically; 4. Execute the cursor; 5. Close the cursor. Example: A cursor can be created cycle-by-circuit to display the names and salaries of the top 10 employees.

How to paginate oracle database How to paginate oracle database Apr 11, 2025 pm 08:42 PM

Oracle database paging uses ROWNUM pseudo-columns or FETCH statements to implement: ROWNUM pseudo-columns are used to filter results by row numbers and are suitable for complex queries. The FETCH statement is used to get the specified number of first rows and is suitable for simple queries.

How to stop oracle database How to stop oracle database Apr 12, 2025 am 06:12 AM

To stop an Oracle database, perform the following steps: 1. Connect to the database; 2. Shutdown immediately; 3. Shutdown abort completely.

What steps are required to configure CentOS in HDFS What steps are required to configure CentOS in HDFS Apr 14, 2025 pm 06:42 PM

Building a Hadoop Distributed File System (HDFS) on a CentOS system requires multiple steps. This article provides a brief configuration guide. 1. Prepare to install JDK in the early stage: Install JavaDevelopmentKit (JDK) on all nodes, and the version must be compatible with Hadoop. The installation package can be downloaded from the Oracle official website. Environment variable configuration: Edit /etc/profile file, set Java and Hadoop environment variables, so that the system can find the installation path of JDK and Hadoop. 2. Security configuration: SSH password-free login to generate SSH key: Use the ssh-keygen command on each node

How to create oracle dynamic sql How to create oracle dynamic sql Apr 12, 2025 am 06:06 AM

SQL statements can be created and executed based on runtime input by using Oracle's dynamic SQL. The steps include: preparing an empty string variable to store dynamically generated SQL statements. Use the EXECUTE IMMEDIATE or PREPARE statement to compile and execute dynamic SQL statements. Use bind variable to pass user input or other dynamic values ​​to dynamic SQL. Use EXECUTE IMMEDIATE or EXECUTE to execute dynamic SQL statements.

How to solve garbled code in oracle How to solve garbled code in oracle Apr 11, 2025 pm 10:09 PM

Oracle garbled problems can be solved by checking the database character set to ensure they match the data. Set the client character set to match the database. Convert data or modify column character sets to match database character sets. Use Unicode character sets and avoid multibyte character sets. Check that the language settings of the database and client are correct.

See all articles