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:
<?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:
<?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等等。