ThinkPHP 連接Oracle數(shù)據(jù)庫的詳細(xì)教程[全]
Jun 13, 2016 am 11:59 AM
一、?操作環(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ù)庫操作工具:PLSQL Developer 32位
下載地址:http://www.allroundautomations.com/plsqldev.html
開發(fā)工具:NetBeans IDE 7.1.2
下載地址:http://netbeans.org/downloads/index.html 下載單PHP的版本即可
說明:這里我反復(fù)強(qiáng)調(diào)軟件的“位”,是因?yàn)檫@個(gè)很重要,一般情況下,我們系統(tǒng)是64位的,那么最好軟件也用64位的,但是這里除過系統(tǒng)外,全部選擇32位是有原因的,目的是為了配合PLSQL Developer和WAMP的PHP擴(kuò)展。因?yàn)镻LSQL Developer沒有64位版。有的朋友說用64位Oracle數(shù)據(jù)庫,裝32位客戶端就行,我不想這樣做,如果不喜歡我的操作方式,可以繞道。當(dāng)然了,如果你不使用PLSQL Developer,而選擇使用Oracle 自帶的SQL Developer,那么你全安裝64位或32都是你自己的事。PHP連接Oracle數(shù)據(jù)庫需要開啟相應(yīng)的擴(kuò)展,這個(gè)擴(kuò)展也需要數(shù)據(jù)庫客戶端的支持,因?yàn)閜hp擴(kuò)展也需要對應(yīng)數(shù)據(jù)庫客戶端的位數(shù)。啰嗦完畢。
二、?環(huán)境配置
1,?操作系統(tǒng)的安裝我就不說了,Oracle安裝自己解決,NetBeans IDE 7.1.2也自己解決。
2,?Wamp的安裝我也不說了,不會(huì)的直接從DOS開始重新學(xué)習(xí)吧。
3,?WAMP會(huì)把PHP的網(wǎng)頁文件夾定義在安裝wamp的文件夾下的www里面,我是安裝在D盤,所以就是D:\WAMP\www。我們暫時(shí)不做別的自定義修改。啟動(dòng)wamp,系統(tǒng)托盤圖標(biāo)為綠色表示啟動(dòng)OK。
4,?打開localhost,看到如下界面,表示環(huán)境配置基本OK。為什么是基本了,因?yàn)檫€沒有對Oracle的配置進(jìn)行設(shè)置。
5,?打開如圖的PHP擴(kuò)展菜單,在綠色圖標(biāo)上,左鍵->PHP->PHP擴(kuò)展,點(diǎn)擊php-oci8的擴(kuò)展,這時(shí)候這個(gè)WAMP會(huì)重啟,等待重啟后變綠,就表示OK。
6,?再次打開剛才的localhost頁面,如果找到如圖4的顯示,就表示目前PHP已經(jīng)支持Oracle了。
注意,我現(xiàn)在用的 wamp和oracle客戶端都是32位,如果其中一個(gè)是64位,那么這個(gè)oci的擴(kuò)展時(shí)打不開的,同時(shí)自動(dòng)環(huán)境監(jiān)測頁面也沒有oci8的顯示。在不使用PL/SQL的前提下,必須是32位Oracle和32位WAMP搭配,64位Oracle和64位WAMP搭配,else請繞道。
三、?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)有這些文件的顯示了,打開index.php,編寫如下內(nèi)容:
復(fù)制代碼 代碼如下:
define('APP_DEBUG', true);
require './ThinkPHP/ThinkPHP.php';
5,?在瀏覽器中打開localhost/項(xiàng)目名/index.php,Thinkphp會(huì)幫你生成好相關(guān)文件和文件夾。
6,?對配置文件進(jìn)行操作,找到:Conf文件夾下config.php文件,修改如下:
復(fù)制代碼 代碼如下:
return array(
'DB_TYPE' => 'Oracle', // 數(shù)據(jù)庫類型
'DB_HOST' => '192.168.0.8', // 服務(wù)器地址
'DB_NAME' => 'orcl', // 數(shù)據(jù)庫名
'DB_USER' => 'test', // 用戶名
'DB_PWD' => 'test', // 密碼
'DB_PORT' => '1521', // 端口
);
Oracle數(shù)據(jù)庫和mysql 的結(jié)構(gòu)不同,一般默認(rèn)安裝的數(shù)據(jù)庫名是orcl,如果你使用了多個(gè)數(shù)據(jù)庫監(jiān)聽,那么就要根據(jù)具體的監(jiān)聽字段來設(shè)置。比如:我本機(jī)數(shù)據(jù)庫堅(jiān)挺是Orcl,同時(shí)監(jiān)聽另外一個(gè)外網(wǎng)的數(shù)據(jù)庫,監(jiān)聽字符串為Orcl2,那么如果你需要連接這個(gè)外網(wǎng)數(shù)據(jù)庫,那么需要寫的數(shù)據(jù)庫名就是orcl2。
7,?經(jīng)過以上的配置,是已經(jīng)可以連接oracle數(shù)據(jù)庫了,但是在thinkphp的實(shí)際操作中有什么注意的地方,且接著往下看。
最近收集了一些關(guān)于THinkPHP連接Oracle數(shù)據(jù)庫的問題,有很多朋友按照連接mysql的方法來操作,導(dǎo)致有一些方法在Oreale中無法正常使用。比如說:findAll,Select方法無法使用,獲取不到需要的數(shù)據(jù)。Create和add方法無法創(chuàng)建和寫入數(shù)據(jù)到數(shù)據(jù)庫中。
其實(shí)根據(jù)以前問題我做了幾天調(diào)試,找到了問題所在,并成功在我自己一個(gè)小項(xiàng)目練習(xí)中使用正常,那么現(xiàn)在就將我的經(jīng)驗(yàn)分享給大家。
1,數(shù)據(jù)庫的連接及配置文件的內(nèi)容我就不說了, 上面已經(jīng)做了解釋。我這里只根據(jù)一個(gè)數(shù)據(jù)表的例子來說明我的操作。
2,表結(jié)構(gòu)如下:
3,這個(gè)表中有3個(gè)字段,ID主鍵,用戶名username和密碼password,因?yàn)閛racle數(shù)據(jù)庫把表名和字段都是轉(zhuǎn)成大寫的,同時(shí)不支持ID主鍵自增,我只有使用另外的方法來實(shí)現(xiàn)這個(gè)功能,比如:ID自動(dòng)序列+觸發(fā)器實(shí)現(xiàn)ID自增。
4,?? ?ThinkPHP中,Action是控制器,Model是模型,視圖是以模板方式體現(xiàn)的。
首先,說控制器,我只做增加和獲取列表的方法介紹。
其次,說模型,這里才是成功的主要原因。為什么?ThinkPHP是有字段映射的,這個(gè)在對MYSQL的支持非常完美,基本不用寫MODEL,但是對ORALCE就不行了,當(dāng)使用M->add()來添加數(shù)據(jù)時(shí),字段會(huì)被$this->_facade()方法過濾掉。這樣生成的SQL語句就是沒法執(zhí)行的,肯定是錯(cuò)誤的,導(dǎo)致數(shù)據(jù)添加不到數(shù)據(jù)庫中,那么使用select()方法也是一樣被過濾。
再次,當(dāng)我單步調(diào)試時(shí),斷點(diǎn)被過濾的時(shí)候,過濾方法使用到了new出來的MODEL,這個(gè)MODEL會(huì)有一個(gè)字段映射的數(shù)組在里面,這個(gè)過濾方法就是和這個(gè)字段數(shù)組進(jìn)行對比,如果不一致就過濾掉,結(jié)果我調(diào)試發(fā)現(xiàn),new出來的MODEL根本沒有把字段映射加進(jìn)去,數(shù)組直接為空,當(dāng)然就沒法和添加的數(shù)據(jù)字段一一對應(yīng)了。這就是錯(cuò)誤的關(guān)鍵。
下面就來說解決方法,其實(shí)很簡單,按照基本的MVC結(jié)構(gòu),不管是PHP還是JAVA還是.NET都有這樣的結(jié)構(gòu),那么按照嚴(yán)格的標(biāo)準(zhǔn),MODEL層的代碼是必須寫的,就是要和數(shù)據(jù)庫的字段做映射。但是很多用mysql的,就直接沒有去寫MODEL里面的代碼。這種習(xí)慣被用到了oracle中,就出了問題。
5,?? ?下面針對我上面的數(shù)據(jù)表寫出我的代碼:
我的Action是這樣的:UserAction.class.php。控制器我只對添加和查找做例子,因此代碼如下:
復(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的原因,一直都這么寫的。針對具體的模型進(jìn)行實(shí)例化,嚴(yán)格規(guī)定我就要對User表進(jìn)行操作了。
獲取POST數(shù)據(jù)的代碼就不多解釋了。
$M_User->create();
這是ThinkPHP的一個(gè)方法,很好,可以幫你過濾掉非法的東西,建議使用。
$Query_Result = $M_User->add($data);
這一段就是數(shù)據(jù)的添加,我習(xí)慣指定要添加的數(shù)據(jù),也是因?yàn)檫@一段需要根據(jù)$M_User實(shí)例化,并過濾字段。當(dāng)然了,我們只要做好MODEL的代碼,就不會(huì)有問題。下面的代碼就不解釋。官方文檔都有。
我的Model是這樣的:UserModel.class.php
protected $fields = array( 'id', 'username', 'password' );
Model解釋:這才是重點(diǎn),這有這樣,new出來的$M_User的映射字段數(shù)組才不會(huì)為空,這樣才能和POST的數(shù)據(jù)進(jìn)行對應(yīng),才會(huì)讓過濾方法正常識別,不被過濾。
6,經(jīng)過了以上的操作,針對Oracle的數(shù)據(jù)庫操作就完成了,我現(xiàn)在也可以任意使用ThinkPHP提供的方法來操作數(shù)據(jù)了,包括分頁(limit),find(),findAll等等。
出處 http://www.cnblogs.com/aceliu/

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

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.

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

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.

Common SQL statements include: 1. CREATETABLE creates tables, such as CREATETABLEemployees(idINTPRIMARYKEY, nameVARCHAR(100), salaryDECIMAL(10,2)); 2. CREATEINDEX creates indexes, such as CREATEINDEXidx_nameONemployees(name); 3. INSERTINTO inserts data, such as INSERTINTO employeees(id, name, salary)VALUES(1,'JohnDoe',75000.00); 4. SELECT check

UseRedisinsteadofatraditionaldatabasewhenyourapplicationrequiresspeedandreal-timedataprocessing,suchasforcaching,sessionmanagement,orreal-timeanalytics.Redisexcelsin:1)Caching,reducingloadonprimarydatabases;2)Sessionmanagement,simplifyingdatahandling

In Oracle database, the steps to configure parallel query to improve performance include: 1. Set at the database level, and implement it by modifying initialization parameters such as PARALLEL_DEGREE_POLICY and PARALLEL_MAX_SERVERS; 2. Set at the session level, adjust the parallelism of the current session through the ALTERSESSION command; 3. Consider key points such as parallelism, resource management and data distribution; 4. Improve performance by optimizing query planning, adjusting parallelism and monitoring and tuning. These steps help to take full advantage of parallel queries and significantly improve the query performance of the database.

The way to view all databases in MongoDB is to enter the command "showdbs". 1. This command only displays non-empty databases. 2. You can switch the database through the "use" command and insert data to make it display. 3. Pay attention to internal databases such as "local" and "config". 4. When using the driver, you need to use the "listDatabases()" method to obtain detailed information. 5. The "db.stats()" command can view detailed database statistics.

The key to installing MySQL 8.0 is to follow the steps and pay attention to common problems. It is recommended to use the MSI installation package on Windows. The steps include downloading the installation package, running the installer, selecting the installation type, setting the root password, enabling service startup, and paying attention to port conflicts or manually configuring the ZIP version; Linux (such as Ubuntu) is installed through apt, and the steps are to update the source, installing the server, running security scripts, checking service status, and modifying the root authentication method; no matter which platform, you should modify the default password, create ordinary users, set up firewalls, adjust configuration files to optimize character sets and other parameters to ensure security and normal use.
