search
HomeBackend DevelopmentPHP TutorialThinkPHP connects to Oracle database, thinkphporacle_PHP tutorial

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';

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', // 端口
);

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> ]');
  }
}

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
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
The Continued Use of PHP: Reasons for Its EnduranceThe Continued Use of PHP: Reasons for Its EnduranceApr 19, 2025 am 12:23 AM

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python: Exploring Their Similarities and DifferencesPHP and Python: Exploring Their Similarities and DifferencesApr 19, 2025 am 12:21 AM

PHP and Python are both high-level programming languages ​​that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP and Python: Different Paradigms ExplainedPHP and Python: Different Paradigms ExplainedApr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP and Python: A Deep Dive into Their HistoryPHP and Python: A Deep Dive into Their HistoryApr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Choosing Between PHP and Python: A GuideChoosing Between PHP and Python: A GuideApr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP and Frameworks: Modernizing the LanguagePHP and Frameworks: Modernizing the LanguageApr 18, 2025 am 12:14 AM

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHP's Impact: Web Development and BeyondPHP's Impact: Web Development and BeyondApr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

How does PHP type hinting work, including scalar types, return types, union types, and nullable types?How does PHP type hinting work, including scalar types, return types, union types, and nullable types?Apr 17, 2025 am 12:25 AM

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values ​​and handle functions that may return null values.

See all articles

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment