


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"
|
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等等。

thinkphp是国产框架。ThinkPHP是一个快速、兼容而且简单的轻量级国产PHP开发框架,是为了简化企业级应用开发和敏捷WEB应用开发而诞生的。ThinkPHP从诞生以来一直秉承简洁实用的设计原则,在保持出色的性能和至简的代码的同时,也注重易用性。

本篇文章给大家带来了关于thinkphp的相关知识,其中主要介绍了关于使用think-queue来实现普通队列和延迟队列的相关内容,think-queue是thinkphp官方提供的一个消息队列服务,下面一起来看一下,希望对大家有帮助。

thinkphp基于的mvc分别是指:1、m是model的缩写,表示模型,用于数据处理;2、v是view的缩写,表示视图,由View类和模板文件组成;3、c是controller的缩写,表示控制器,用于逻辑处理。mvc设计模式是一种编程思想,是一种将应用程序的逻辑层和表现层进行分离的方法。

本篇文章给大家带来了关于thinkphp的相关知识,其中主要介绍了使用jwt认证的问题,下面一起来看一下,希望对大家有帮助。

thinkphp扩展有:1、think-migration,是一种数据库迁移工具;2、think-orm,是一种ORM类库扩展;3、think-oracle,是一种Oracle驱动扩展;4、think-mongo,一种MongoDb扩展;5、think-soar,一种SQL语句优化扩展;6、porter,一种数据库管理工具;7、tp-jwt-auth,一个jwt身份验证扩展包。

thinkphp查询库是否存在的方法:1、打开相应的tp文件;2、通过“ $isTable=db()->query('SHOW TABLES LIKE '."'".$data['table_name']."'");if($isTable){...}else{...}”方式验证表是否存在即可。

本篇文章给大家带来了关于ThinkPHP的相关知识,其中主要整理了使用think-queue实现redis消息队列的相关问题,下面一起来看一下,希望对大家有帮助。

在thinkphp3.2中,可以利用define关闭调试模式,该标签用于变量和常量的定义,将入口文件中定义调试模式设为FALSE即可,语法为“define('APP_DEBUG', false);”;开启调试模式将参数值设置为true即可。


Hot AI Tools

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

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

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Linux new version
SublimeText3 Linux latest version

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

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

Dreamweaver CS6
Visual web development tools
