thinkphp Connecting to Oracle database, thinkphporacle_php tutorial

Source: Internet
Author: User
Tags php define netbeans

thinkphp Connecting to Oracle database, thinkphporacle


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.bkjia.com/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 Operations 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: Here I repeatedly emphasize the "bit" of software, because this is very important, in general, our system is 64-bit, then the best software is also used 64-bit, 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 extension. Because Plsql developer does not have a 64-bit version. Some friends say with 64 Oracle database, install 32-bit client on the line, I do not want to do so, if I do not like the way I operate, you can bypass. Of course, if you don't use Plsql Developer and choose to use Oracle's own SQL Developer, it's your business to install 64-bit or 32. The PHP connection to the Oracle database requires a corresponding extension, which also requires the support of the database client, because the PHP extension also requires the corresponding number of bits for the database client. Verbose.

Second, the Environment configuration

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

2,wamp installation I also do not say, will not be directly from the DOS began to re-study it.

3,wamp will be the PHP page folder defined in the installation WAMP folder under the WWW, I was installed on the D disk, so is D:\WAMP\www. We do not make any other custom changes for the time being. Start Wamp, the system tray icon is green to indicate the boot OK.

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

5, open the PHP extension menu, on the green icon, left->php->php extension, click on the extension of Php-oci8, this time this wamp will restart, wait for the restart to turn green, it means OK.

6. Open the localhost page again, if you find the 4 display, it means that PHP is already supported by Oracle.

Note that I now use the Wamp and Oracle client are 32-bit, if one of the 64-bit, then the OCI extension will not open, while the automatic Environment Monitoring page is not oci8 display. Without PL/SQL, you must be 32-bit Oracle and 32-bit WAMP, 64-bit Oracle and 64-bit wamp, else please take a detour.

Three, thinkphp configuration

1, the Download Good 3.0 official version decompression, the project only need thinkphp folder, this is the core.
2, using the IDE to create a new project, the project folder is just under the Wamp www folder, if the individual needs to customize other folders, need to modify the Apache configuration file, here I do not modify.
3. Copy the thinkphp folder to the project folder, create a new PHP file, and name the index.php.
4,ide already have these files displayed, open index.php, write the following:

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

5. Opening the localhost/project name in the browser/index.php,thinkphp will help you generate the relevant files and folders.
6, to operate the configuration file, locate: Conf folder under the config.php file, modify the following:

<?phpreturn Array (' db_type ' = ' Oracle ',//database type ' db_host ' = ' 192.168.0.8 ',//server address ' db_name ' = ' orcl ',/ /database name ' db_user ' + ' test ',//user name ' 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 multiple database listening, then the specific listening field to set. For example: My native database firm is ORCL, while listening to another external network database, listening string for Orcl2, then if you need to connect to this extranet database, then the database name to write is Orcl2.

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

Recently collected some questions about thinkphp connection to Oracle database, many friends follow the method of connection to MySQL, cause some methods in Oreale not normal use. For example: The Findall,select method is not available, and the required data is not available. The Create and add methods cannot be created and write data to the database.

In fact, according to the previous question I did a few days debugging, found the problem, and success in my own small project practice to use Normal, then I will now share my experience to everyone.

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

2, the table structure is as follows:

3, the table has 3 fields, ID primary key, user name username and password password, because the Oracle database to the table name and field are all uppercase, and do not support ID primary key auto-increment, I only use another method to implement this function, such as: ID automatic sequence + The trigger implementation ID is self-increment.

4, in thinkphp, the action is the controller, model is the mode, and the view is represented by the template.

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

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

Again, when I single-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 inside, this filter method is compared with this field array, if the inconsistency is filtered out, the result I debug found that, The new model does not add a field map, the array is empty, and of course it cannot correspond to the added data field one by one. This is the key to the mistake.

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

5, write my code below for the data sheet above me:

My action is this: UserAction.class.php. Controller I only do examples of additions and lookups, so the code is as follows:

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 Submit process 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 added succeeded '),   } else {    $this->error (' user added error ');   }  } else {   header ("content-type:text/html; Charset=utf-8 ");   Exit ($M _user->geterror (). ' [Return] ');}  }

Action Explanation:

$M _user=new Usermodel ();

This method is best written because of doing. NET for the reason, has always been so written. To instantiate a specific model, it is strictly stipulated that I am going to operate on the user table.

The code to get the post data is not much explained.

$M _user->create ();

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

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

This paragraph 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 model code, there will be no problem. The following code is not explained. Official documents are available.

My model is this: UserModel.class.php

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

Model explanation: This is the focus, this has this, new out of the $m_user map field array will not be empty, so that the post data to correspond to the filter method will be normal recognition, not filtered.

6, after the above operation, the database operation for Oracle completed, I can now arbitrarily use the method provided by thinkphp to manipulate the data, including paging (limit), find (), FindAll and so on.

http://www.bkjia.com/PHPjc/1122892.html www.bkjia.com true http://www.bkjia.com/PHPjc/1122892.html techarticle thinkphp Connection to Oracle database, thinkphporacle One, operating environment Building system: Windows7 Ultimate Edition 64-bit PHP Environment: wampserver2.2e-php5.4.3-httpd2.2.22-mysql5.5.24 32-bit version download ...

  • 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.