ThinkPHP detailed tutorial Oracle database connection

First, the operating environment to build

systems: Windows7 Ultimate 64-bit
PHP environment: wampserver2.2e-php5.4.3-httpd2.2.22-mysql5.5.24 32 -bit version
ThinkPHP: 3.0 official version of
Oracle: Orcale_11gR2 32-bit version of
the database manipulation tools: PLSQL Developer 32-bit
development tools: NetBeans IDE 7.1.2

Description: here I have repeatedly stressed that "bit" software, this is very important because, under normal circumstances, our system is 64-bit, it is best to use 64-bit software, but here outside the system through the addition, all 32-bit choice there is a reason, with the purpose PLSQL Developer and WAMP PHP extension. PLSQL Developer because no 64-bit version. Some friends said that with 64-bit Oracle database, install 32-bit client on the line, I do not want to do this if I do not like the mode of operation, can pass. Of course, if you do not use PLSQL Developer, and choose to use Oracle's own SQL Developer, then you are your own business full installation of 64 or 32. PHP connecting Oracle database need to open the appropriate extension, this extension also needs to support database clients, because php extensions also need to correspond to the number of bits database client. Long-winded completed.

Second, the environment configuration

1, the operating system installation I will not say, Oracle installation themselves, NetBeans IDE 7.1.2 also themselves.

2, Wamp installation I do not say, do not start from DOS to re-learn it directly.

3, WAMP will PHP web www folder definition files are installed in the wamp folder inside, I was installed in the D drive, so is D: \ WAMP \ www. We do not do other custom modifications. Start wamp, the system tray icon indicates OK to start the green.


4, open localhost, see the following interface, the basic configuration is the ambient OK. Why is basic, because Oracle has not set the configuration. 5, PHP open the extended menu as shown in the green icon, button -> PHP-> PHP extension, click the extension php-oci8, this time the WAMP will restart, after waiting for restart green, it means OK. 6, open the page again just localhost, if the display shown in Figure 4 is found, it means the current PHP has support for the Oracle. Note that I am using wamp and oracle client is 32, if one of them is 64, then open when the oci extension, while automatic environmental monitoring page does not display the oci8. Without the use of PL / SQL, Oracle must be 32-bit and 32-bit WAMP mix of 64-bit and 64-bit Oracle WAMP match, else please pass.








Links: https://pan.baidu.com/s/1v5gm7n0L7TGyejCmQrMh2g  extraction code: x2p5

free to share, but serious limitations of X, should click on the link or links fail Search plus population group number 936 682 608 .


Three, ThinkPHP configuration

1, the official version 3.0 to download a good decompression, only ThinkPHP project folder, which is the core.
2, use the IDE to create a new project folder of the project is just under Wamp the www folder, if you need to customize other personal folders, you need to modify the apache configuration file. I do not modify here.
3, Thinkphp folder to the project folder, create a php file, named index.php.
4, IDE has been displayed in these files, open the index.php, prepare the following:
Code as follows:

  1. php
  2. define('APP_DEBUG', true);
  3.  require './ThinkPHP/ThinkPHP.php';
复制代码

5, 在浏览器中打开localhost/项目名/index.php,Thinkphp会帮你生成好相关文件和文件夹。
6, 对配置文件进行操作,找到:Conf文件夹下config.php文件,修改如下:
. 代码如下:

  1. php
  2.  return array(
  3.  'DB_TYPE' => 'Oracle', // 数据库类型
  4.  'DB_HOST' => '192.168.0.8', // 服务器地址
  5.  'DB_NAME' => 'orcl', // 数据库名
  6.  'DB_USER' => 'test', // 用户名
  7.  'DB_PWD' => 'test', // 密码
  8.  'DB_PORT' => '1521', // 端口
  9.  );
复制代码

Oracle数据库和mysql 的结构不同,一般默认安装的数据库名是orcl,如果你使用了多个数据库监听,那么就要根据具体的监听字段来设置。比如:我本机数据库坚挺是Orcl,同时监听另外一个外网的数据库,监听字符串为Orcl2,那么如果你需要连接这个外网数据库,那么需要写的数据库名就是orcl2。

7, 经过以上的配置,是已经可以连接oracle数据库了,但是在thinkphp的实际操作中有什么注意的地方,且接着往下看。

最近收集了一些关于THinkPHP连接Oracle数据库的问题,有很多朋友按照连接mysql的方法来操作,导致有一些方法在Oreale中无法正常使用。比如说:findAll,Select方法无法使用,获取不到需要的数据。Create和add方法无法创建和写入数据到数据库中。

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

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。控制器我只对添加和查找做例子,因此代码如下:
. 代码如下:

  1. public function index() {
  2. header("Content-Type:text/html; charset=utf-8");
  3. $M_User = new UserModel();
  4. $User_List = $M_User->select();
  5. $this->assign('Title', '用户管理');
  6. $this->assign('UserList', $User_List);
  7. $this->display();
  8.  }
  9.  //添加用户提交处理
  10.  public function Create_Post() {
  11. $M_User = new UserModel();
  12. $data['username'] = $this->_post('username');
  13. $data['password'] = md5($this->_post('pwd'));
  14.  if ($M_User->create()) {
  15. $Query_Result = $M_User->add($data);
  16.  if (false !== $Query_Result) {
  17. $this->success('用户添加成功');
  18.  } else {
  19. $this->error('用户添加错误');
  20.  }
  21.  } else {
  22. header("Content-Type:text/html; charset=utf-8");
  23.  exit($M_User->getError() . ' [ 返 回 ]');
  24.  }
  25.  }
复制代码

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 $fields = array( 'id', 'username', 'password' );

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

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

Guess you like

Origin www.cnblogs.com/it-3327/p/11726794.html