thinkphp Connect Oracle Database _php Tips

Source: Internet
Author: User
Tags php file netbeans

First, the operating environment to build

System: WINDOWS7 flagship 64-bit

PHP Environment: wampserver2.2e-php5.4.3-httpd2.2.22-mysql5.5.24 32-bit version
Download Address: http://www.jb51.net/softs/161568.html

thinkphp:3.2.3 Official Edition
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 tools: Plsql Developer 32-bit
Download Address: http://www.jb51.net/softs/63962.html

Development tools: NetBeans IDE 7.1.2
Download Address: http://www.jb51.net/softs/18343.html

Description: Here I repeatedly emphasize the "bit" of software, is because this is very important, under normal circumstances, our system is 64 bits, then the best software also use 64 bits, but here in addition to the system, all choose 32 bits for a reason, the purpose is to cooperate with Plsql developer and Wamp PHP expansion. Because Plsql developer has no 64-bit version. Some friends say with 64-bit Oracle database, 32-bit client on the line, I do not want to do, if I do not like the way I operate, you can bypass. Of course, if you don't use Plsql Developer, and you choose to use Oracle's own SQL Developer, it's your business to install 64 or 32 of them all. PHP to connect to the Oracle database needs to open the appropriate extension, this extension also requires the support of the database client, because the PHP extension also requires the corresponding database client number of digits. Verbose finish.

Second, the Environment configuration

1, the installation of the operating system I do not say, Oracle installation itself resolved, NetBeans IDE 7.1.2 also solve their own.

2,wamp installation I do not say, will not be directly from the DOS start to learn again.

3,wamp will define the PHP Web folder in the WAMP folder under the WWW, I was installed in D disk, so is D:\WAMP\www. We don't make any custom modifications for the time being. Start Wamp, the system tray icon is green to start OK.

4, open localhost, see the following interface, indicating that the environment configuration basic OK. Why is basic, because the Oracle configuration has not yet been set up.

5, open as the diagram of the PHP expansion menu, on the green icon, left->php->php expansion, click on the expansion of PHP-OCI8, this time the Wamp will restart, waiting for the restart after the green, it means OK.

6, open just the localhost page again, if found as shown in Figure 4, it means that the current PHP has supported Oracle.

Note that the Wamp and Oracle clients I use now are 32-bit, and if one of them is 64, the OCI cannot be expanded and the automatic Environment Monitoring page has no oci8 display. Without the use of pl/sql, it must be 32-bit Oracle and 32-bit Wamp collocation, 64-bit Oracle and 64-bit wamp collocation, else please bypass.

Third, thinkphp configuration

1, the download of the 3.0 official version of the release, the project only needs thinkphp folder, which is the core.
2, use the IDE to create a new project, the folder for the project is just under the Wamp www folder, if the individual needs to customize other folders, need to modify the Apache configuration file, I do not modify here.
3, copy the thinkphp folder to the project folder, create a new PHP file, and name index.php.
These files are already displayed in 4,ide, open index.php, and write the following:

<?php
 define (' App_debug ', true);
 Require './thinkphp/thinkphp.php ';

5, open the localhost/project name in the browser/index.php,thinkphp will help you build the relevant files and folders.
6, the configuration file to operate, find: Conf folder under the Config.php file, modified as follows:

<?php return
Array (
 ' db_type ' => ' Oracle ',//database type
 ' db_host ' => ' 192.168.0.8 ',//server address
 ' db_ Name ' => ' ORCL ',//database name
 ' db_user ' => ' test ',//username
 ' db_pwd ' => ' test ',//password
 ' db_port ' => ' 1521 ',//port
);

Oracle database and MySQL structure is different, the general default installation of the database name is ORCL, if you use a number of database monitoring, then you need to set according to the specific listening field. For example: My local database firm is ORCL, while listening to another extranet database, listening to the string for Orcl2, then if you need to connect this extranet database, then need to write the database name is Orcl2.

7, after the above configuration, is already able to connect Oracle database, but in the actual operation of thinkphp have any attention, and listen to let's.

Recently collected some questions about thinkphp connection Oracle database, there are many friends in accordance with the method to connect MySQL operation, resulting in some methods in the Oreale can not be used normally. For example, the Findall,select method cannot be used to obtain the required data. The Create and add methods cannot be created and written to the database.

In fact, according to the previous problems I have done a few days debugging, found the problem, and successfully in my own small project to use the normal practice, then I will now share my experience.

1, the database connection and the contents of the configuration file I do not say, the above has been explained. I'm only here to illustrate my operation based on an example of a datasheet.

2, the table structure is as follows:

3, there are 3 fields in this table, ID primary key, username username and password password, because the Oracle database to the table name and fields are capitalized, and does not support the ID of the primary key to increase, I only use a different method to achieve this function, such as: ID automatic sequence + The trigger implementation ID is self increasing.

4, in thinkphp, the action is the controller, model is modeled, the view is embodied in the template way.

First of all, say controller, I only do add and get list of methods introduced.

Secondly, to say the model, here is the main reason for success. Why? thinkphp is a field map, this in the MySQL support is very perfect, basically do not have to write model, but for Oralce, when using M->add () to add data, the field will be $this->_facade () method filter out. This generated SQL statement is not executable, is certainly wrong, resulting in the data is not added to the database, then the use of the Select () method is also filtered.

Again, when I step debugging, when the breakpoint is filtered, the filter method used to the new model, the model will have a field map of the array in the inside, this filtering method is compared with the field array, if inconsistent on the filter, the result I debug found, The new model does not add the field mapping, the array directly empty, of course, and added data field one by one corresponding. That is the key to the mistake.

The following is the solution, in fact very simple, according to the basic MVC structure, whether it is PHP or Java or. NET have such a structure, then according to strict standards, the model layer of code must be written, is to and the database fields to do mapping. But a lot of MySQL, it is not directly to write model inside the code. This habit was used in Oracle, and there was a problem.

5, write down my code for the data table above:

My action is this: UserAction.class.php. Controller I only do an example of adding and finding, so the code is as follows:

The Public Function index () {
  header (content-type:text/html; Charset=utf-8 ");
 
  $M _user = new Usermodel ();
 
  $User _list = $M _user->select ();
 
  $this->assign (' Title ', ' User Management ');
 
  $this->assign (' userlist ', $User _list);
 
  $this->display ();
}
 
 Add user commit processing 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 (' user adds success ');
   } else {
    $this->error (' User add error ');
   }
  else {
   header ("content-type:text/html; Charset=utf-8 ");
   Exit ($M _user->geterror (). ' [<a href= ' Javascript:history.back () ' > Return </a>] ');
  }

Action Explanation:

$M _user=new Usermodel ();

This method is best written because it is done. NET reason, has been so written. For the specific model of the instantiation, it is strictly stipulated that I will be operating on the user table.

The code that gets the post data is not much explained.

$M _user->create ();

This is a method of thinkphp, very good, can help you to filter out illegal things, recommended to use.

$Query _result = $M _user->add ($data);

This section is the addition of data, I am accustomed to specifying the data to be added, also because this paragraph needs to be instantiated according to $m_user, and filter the fields. Of course, as long as we do the code model, there will be no problem. The following code does not explain. Official documents are available.

My model is like this: UserModel.class.php

'id', 'username', 'password');

Model explanation: This is the focus, this is so, new out of the $m_user mapping field array will not be empty, so as to and post data to correspond, will let the filtering method is normally recognized, not filtered.

6, after the above operation, the database operation for Oracle is complete, and I can now also use the thinkphp provided to manipulate the data, including pagination (limit), find (), FindAll and so on.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.