国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

Home php教程 php手冊(cè) ThinkPHP 連接Oracle數(shù)據(jù)庫(kù)的詳細(xì)教程[全]

ThinkPHP 連接Oracle數(shù)據(jù)庫(kù)的詳細(xì)教程[全]

Jun 13, 2016 am 11:59 AM
oracle thinkphp windows7 one build operate Tutorial database flagship environment of system detailed connect

一、?操作環(huán)境搭建

系統(tǒng):Windows7 旗艦版 64位
PHP環(huán)境:wampserver2.2e-php5.4.3-httpd2.2.22-mysql5.5.24? 32位版
下載地址:http://www.wampserver.com/en/


ThinkPHP:3.0正式版
下載地址:http://thinkphp.cn/down.html


Oracle:Orcale_11gR2? 32位版
下載地址:http://www.oracle.com/technetwork/cn/indexes/downloads/index.html


數(shù)據(jù)庫(kù)操作工具:PLSQL Developer 32位
下載地址:http://www.allroundautomations.com/plsqldev.html


開(kāi)發(fā)工具:NetBeans IDE 7.1.2
下載地址:http://netbeans.org/downloads/index.html 下載單PHP的版本即可

說(shuō)明:這里我反復(fù)強(qiáng)調(diào)軟件的“位”,是因?yàn)檫@個(gè)很重要,一般情況下,我們系統(tǒng)是64位的,那么最好軟件也用64位的,但是這里除過(guò)系統(tǒng)外,全部選擇32位是有原因的,目的是為了配合PLSQL Developer和WAMP的PHP擴(kuò)展。因?yàn)镻LSQL Developer沒(méi)有64位版。有的朋友說(shuō)用64位Oracle數(shù)據(jù)庫(kù),裝32位客戶端就行,我不想這樣做,如果不喜歡我的操作方式,可以繞道。當(dāng)然了,如果你不使用PLSQL Developer,而選擇使用Oracle 自帶的SQL Developer,那么你全安裝64位或32都是你自己的事。PHP連接Oracle數(shù)據(jù)庫(kù)需要開(kāi)啟相應(yīng)的擴(kuò)展,這個(gè)擴(kuò)展也需要數(shù)據(jù)庫(kù)客戶端的支持,因?yàn)閜hp擴(kuò)展也需要對(duì)應(yīng)數(shù)據(jù)庫(kù)客戶端的位數(shù)。啰嗦完畢。

二、?環(huán)境配置

1,?操作系統(tǒng)的安裝我就不說(shuō)了,Oracle安裝自己解決,NetBeans IDE 7.1.2也自己解決。

2,?Wamp的安裝我也不說(shuō)了,不會(huì)的直接從DOS開(kāi)始重新學(xué)習(xí)吧。

3,?WAMP會(huì)把PHP的網(wǎng)頁(yè)文件夾定義在安裝wamp的文件夾下的www里面,我是安裝在D盤,所以就是D:\WAMP\www。我們暫時(shí)不做別的自定義修改。啟動(dòng)wamp,系統(tǒng)托盤圖標(biāo)為綠色表示啟動(dòng)OK。

4,?打開(kāi)localhost,看到如下界面,表示環(huán)境配置基本OK。為什么是基本了,因?yàn)檫€沒(méi)有對(duì)Oracle的配置進(jìn)行設(shè)置。

5,?打開(kāi)如圖的PHP擴(kuò)展菜單,在綠色圖標(biāo)上,左鍵->PHP->PHP擴(kuò)展,點(diǎn)擊php-oci8的擴(kuò)展,這時(shí)候這個(gè)WAMP會(huì)重啟,等待重啟后變綠,就表示OK。

6,?再次打開(kāi)剛才的localhost頁(yè)面,如果找到如圖4的顯示,就表示目前PHP已經(jīng)支持Oracle了。

注意,我現(xiàn)在用的 wamp和oracle客戶端都是32位,如果其中一個(gè)是64位,那么這個(gè)oci的擴(kuò)展時(shí)打不開(kāi)的,同時(shí)自動(dòng)環(huán)境監(jiān)測(cè)頁(yè)面也沒(méi)有oci8的顯示。在不使用PL/SQL的前提下,必須是32位Oracle和32位WAMP搭配,64位Oracle和64位WAMP搭配,else請(qǐng)繞道。

三、?ThinkPHP配置

1,?把下載好的3.0正式版解壓,項(xiàng)目中只需要ThinkPHP文件夾,這是核心。
2,?使用IDE新建一個(gè)項(xiàng)目,項(xiàng)目的文件夾為剛才的Wamp下的www文件夾,如果個(gè)人需要自定義別的文件夾,需要修改apache的配置文件,這里我不修改。
3,?將Thinkphp文件夾拷貝到項(xiàng)目文件夾中,新建一個(gè)php文件,命名index.php。
4,?IDE中已經(jīng)有這些文件的顯示了,打開(kāi)index.php,編寫如下內(nèi)容:

復(fù)制代碼 代碼如下:


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


5,?在瀏覽器中打開(kāi)localhost/項(xiàng)目名/index.php,Thinkphp會(huì)幫你生成好相關(guān)文件和文件夾。
6,?對(duì)配置文件進(jìn)行操作,找到:Conf文件夾下config.php文件,修改如下:

復(fù)制代碼 代碼如下:


return array(
'DB_TYPE' => 'Oracle', // 數(shù)據(jù)庫(kù)類型
'DB_HOST' => '192.168.0.8', // 服務(wù)器地址
'DB_NAME' => 'orcl', // 數(shù)據(jù)庫(kù)名
'DB_USER' => 'test', // 用戶名
'DB_PWD' => 'test', // 密碼
'DB_PORT' => '1521', // 端口
);


Oracle數(shù)據(jù)庫(kù)和mysql 的結(jié)構(gòu)不同,一般默認(rèn)安裝的數(shù)據(jù)庫(kù)名是orcl,如果你使用了多個(gè)數(shù)據(jù)庫(kù)監(jiān)聽(tīng),那么就要根據(jù)具體的監(jiān)聽(tīng)字段來(lái)設(shè)置。比如:我本機(jī)數(shù)據(jù)庫(kù)堅(jiān)挺是Orcl,同時(shí)監(jiān)聽(tīng)另外一個(gè)外網(wǎng)的數(shù)據(jù)庫(kù),監(jiān)聽(tīng)字符串為Orcl2,那么如果你需要連接這個(gè)外網(wǎng)數(shù)據(jù)庫(kù),那么需要寫的數(shù)據(jù)庫(kù)名就是orcl2。

7,?經(jīng)過(guò)以上的配置,是已經(jīng)可以連接oracle數(shù)據(jù)庫(kù)了,但是在thinkphp的實(shí)際操作中有什么注意的地方,且接著往下看。

最近收集了一些關(guān)于THinkPHP連接Oracle數(shù)據(jù)庫(kù)的問(wèn)題,有很多朋友按照連接mysql的方法來(lái)操作,導(dǎo)致有一些方法在Oreale中無(wú)法正常使用。比如說(shuō):findAll,Select方法無(wú)法使用,獲取不到需要的數(shù)據(jù)。Create和add方法無(wú)法創(chuàng)建和寫入數(shù)據(jù)到數(shù)據(jù)庫(kù)中。

其實(shí)根據(jù)以前問(wèn)題我做了幾天調(diào)試,找到了問(wèn)題所在,并成功在我自己一個(gè)小項(xiàng)目練習(xí)中使用正常,那么現(xiàn)在就將我的經(jīng)驗(yàn)分享給大家。

1,數(shù)據(jù)庫(kù)的連接及配置文件的內(nèi)容我就不說(shuō)了, 上面已經(jīng)做了解釋。我這里只根據(jù)一個(gè)數(shù)據(jù)表的例子來(lái)說(shuō)明我的操作。

2,表結(jié)構(gòu)如下:



3,這個(gè)表中有3個(gè)字段,ID主鍵,用戶名username和密碼password,因?yàn)閛racle數(shù)據(jù)庫(kù)把表名和字段都是轉(zhuǎn)成大寫的,同時(shí)不支持ID主鍵自增,我只有使用另外的方法來(lái)實(shí)現(xiàn)這個(gè)功能,比如:ID自動(dòng)序列+觸發(fā)器實(shí)現(xiàn)ID自增。

4,?? ?ThinkPHP中,Action是控制器,Model是模型,視圖是以模板方式體現(xiàn)的。

首先,說(shuō)控制器,我只做增加和獲取列表的方法介紹。

其次,說(shuō)模型,這里才是成功的主要原因。為什么?ThinkPHP是有字段映射的,這個(gè)在對(duì)MYSQL的支持非常完美,基本不用寫MODEL,但是對(duì)ORALCE就不行了,當(dāng)使用M->add()來(lái)添加數(shù)據(jù)時(shí),字段會(huì)被$this->_facade()方法過(guò)濾掉。這樣生成的SQL語(yǔ)句就是沒(méi)法執(zhí)行的,肯定是錯(cuò)誤的,導(dǎo)致數(shù)據(jù)添加不到數(shù)據(jù)庫(kù)中,那么使用select()方法也是一樣被過(guò)濾。

再次,當(dāng)我單步調(diào)試時(shí),斷點(diǎn)被過(guò)濾的時(shí)候,過(guò)濾方法使用到了new出來(lái)的MODEL,這個(gè)MODEL會(huì)有一個(gè)字段映射的數(shù)組在里面,這個(gè)過(guò)濾方法就是和這個(gè)字段數(shù)組進(jìn)行對(duì)比,如果不一致就過(guò)濾掉,結(jié)果我調(diào)試發(fā)現(xiàn),new出來(lái)的MODEL根本沒(méi)有把字段映射加進(jìn)去,數(shù)組直接為空,當(dāng)然就沒(méi)法和添加的數(shù)據(jù)字段一一對(duì)應(yīng)了。這就是錯(cuò)誤的關(guān)鍵。

下面就來(lái)說(shuō)解決方法,其實(shí)很簡(jiǎn)單,按照基本的MVC結(jié)構(gòu),不管是PHP還是JAVA還是.NET都有這樣的結(jié)構(gòu),那么按照嚴(yán)格的標(biāo)準(zhǔn),MODEL層的代碼是必須寫的,就是要和數(shù)據(jù)庫(kù)的字段做映射。但是很多用mysql的,就直接沒(méi)有去寫MODEL里面的代碼。這種習(xí)慣被用到了oracle中,就出了問(wèn)題。

5,?? ?下面針對(duì)我上面的數(shù)據(jù)表寫出我的代碼:

我的Action是這樣的:UserAction.class.php。控制器我只對(duì)添加和查找做例子,因此代碼如下:

復(fù)制代碼 代碼如下:


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('用戶添加錯(cuò)誤');
}
} else {
header("Content-Type:text/html; charset=utf-8");
exit($M_User->getError() . ' [ 返 回 ]');
}
}


?Action解釋:

$M_User=new UserModel();

這個(gè)方法最好這么寫,因?yàn)樽?NET的原因,一直都這么寫的。針對(duì)具體的模型進(jìn)行實(shí)例化,嚴(yán)格規(guī)定我就要對(duì)User表進(jìn)行操作了。

獲取POST數(shù)據(jù)的代碼就不多解釋了。

$M_User->create();

這是ThinkPHP的一個(gè)方法,很好,可以幫你過(guò)濾掉非法的東西,建議使用。

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

這一段就是數(shù)據(jù)的添加,我習(xí)慣指定要添加的數(shù)據(jù),也是因?yàn)檫@一段需要根據(jù)$M_User實(shí)例化,并過(guò)濾字段。當(dāng)然了,我們只要做好MODEL的代碼,就不會(huì)有問(wèn)題。下面的代碼就不解釋。官方文檔都有。

我的Model是這樣的:UserModel.class.php

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

Model解釋:這才是重點(diǎn),這有這樣,new出來(lái)的$M_User的映射字段數(shù)組才不會(huì)為空,這樣才能和POST的數(shù)據(jù)進(jìn)行對(duì)應(yīng),才會(huì)讓過(guò)濾方法正常識(shí)別,不被過(guò)濾。

6,經(jīng)過(guò)了以上的操作,針對(duì)Oracle的數(shù)據(jù)庫(kù)操作就完成了,我現(xiàn)在也可以任意使用ThinkPHP提供的方法來(lái)操作數(shù)據(jù)了,包括分頁(yè)(limit),find(),findAll等等。

出處 http://www.cnblogs.com/aceliu/

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

Undress AI Tool

Undress AI Tool

Undress images for free

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.

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)

MySQL vs. Oracle: Licensing, Features, and Benefits MySQL vs. Oracle: Licensing, Features, and Benefits May 08, 2025 am 12:05 AM

The main difference between MySQL and Oracle is licenses, features, and advantages. 1. License: MySQL provides a GPL license for free use, and Oracle adopts a proprietary license, which is expensive. 2. Function: MySQL has simple functions and is suitable for web applications and small and medium-sized enterprises. Oracle has powerful functions and is suitable for large-scale data and complex businesses. 3. Advantages: MySQL is open source free, suitable for startups, and Oracle is reliable in performance, suitable for large enterprises.

Redis: A Comparison to Traditional Database Servers Redis: A Comparison to Traditional Database Servers May 07, 2025 am 12:09 AM

Redis is superior to traditional databases in high concurrency and low latency scenarios, but is not suitable for complex queries and transaction processing. 1.Redis uses memory storage, fast read and write speed, suitable for high concurrency and low latency requirements. 2. Traditional databases are based on disk, support complex queries and transaction processing, and have strong data consistency and persistence. 3. Redis is suitable as a supplement or substitute for traditional databases, but it needs to be selected according to specific business needs.

How to learn Java without taking detours. Share methods and techniques for efficiently learning Java How to learn Java without taking detours. Share methods and techniques for efficiently learning Java May 20, 2025 pm 08:24 PM

The key to learning Java without taking detours is: 1. Understand core concepts and grammar; 2. Practice more; 3. Understand memory management and garbage collection; 4. Join online communities; 5. Read other people’s code; 6. Understand common libraries and frameworks; 7. Learn to deal with common mistakes; 8. Make a learning plan and proceed step by step. These methods can help you master Java programming efficiently.

Is Redis Primarily a Database? Is Redis Primarily a Database? May 05, 2025 am 12:07 AM

Redis is primarily a database, but it is more than just a database. 1. As a database, Redis supports persistence and is suitable for high-performance needs. 2. As a cache, Redis improves application response speed. 3. As a message broker, Redis supports publish-subscribe mode, suitable for real-time communication.

What to learn Java? A summary of Java learning routes and essential knowledge points What to learn Java? A summary of Java learning routes and essential knowledge points May 20, 2025 pm 08:15 PM

Learning Java requires learning basic syntax, object-oriented programming, collection frameworks, exception handling, multithreading, I/O streaming, JDBC, network programming, and advanced features such as reflection and annotation. 1. The basic syntax includes variables, data types, operators and control flow statements. 2. Object-oriented programming covers classes, objects, inheritance, polymorphism, encapsulation and abstraction. 3. The collection framework involves ArrayList, LinkedList, HashSet, and HashMap. 4. Exception handling ensures program robustness through try-catch block. 5. Multithreaded programming requires understanding of thread life cycle and synchronization. 6. I/O streams are used for data reading, writing and file operations. 7. JDBC is used to interact with databases. 8. Network programming passes S

Oracle Software: Maximizing Efficiency and Performance Oracle Software: Maximizing Efficiency and Performance May 06, 2025 am 12:07 AM

Oracle software can improve performance in a variety of ways. 1) Optimize SQL queries and reduce data transmission; 2) Appropriately manage indexes to balance query speed and maintenance costs; 3) Reasonably configure memory, optimize SGA and PGA; 4) Reduce I/O operations and use appropriate storage devices.

Connection and data visualization of Oracle databases with BI tools such as Tableau Connection and data visualization of Oracle databases with BI tools such as Tableau May 19, 2025 pm 06:27 PM

To connect Oracle database to Tableau for data visualization, you need to follow the following steps: 1. Configure Oracle database connection in Tableau, use ODBC or JDBC drivers; 2. Explore data and create visualizations, such as bar charts, etc.; 3. Optimize SQL queries and indexes to improve performance; 4. Use Oracle's complex data types and functions to implement through custom SQL queries; 5. Create materialized views to improve query speed; 6. Use Tableau's interactive functions such as dashboard for in-depth analysis.

MongoDB vs. Oracle: Exploring NoSQL and Relational Approaches MongoDB vs. Oracle: Exploring NoSQL and Relational Approaches May 07, 2025 am 12:02 AM

In different application scenarios, choosing MongoDB or Oracle depends on specific needs: 1) If you need to process a large amount of unstructured data and do not have high requirements for data consistency, choose MongoDB; 2) If you need strict data consistency and complex queries, choose Oracle.

See all articles