Home Backend Development PHP Tutorial ThinkPHP Detailed Tutorial on Connecting to Oracle Database [Full]_PHP Tutorial

ThinkPHP Detailed Tutorial on Connecting to Oracle Database [Full]_PHP Tutorial

Jul 21, 2016 pm 03:17 PM
oracle thinkphp windows7 one build operate Tutorial database flagship environment of system detailed connect

1. Operating environment construction

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 extension of php-oci8. At this time, the WAMP will restart and wait until it turns green. 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:

Copy the code The code is as follows:

define('APP_DEBUG', true);
require './ThinkPHP/ThinkPHP.php';

5, in the browser Open localhost/project name/index.php and Thinkphp will generate relevant files and folders for you.
6. Operate the configuration file, find: config.php file in the Conf folder, modify it as follows:
Copy the code The code is as follows:

return 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
);

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, but what should you pay attention to in the actual operation of thinkphp? Let’s take a look below.

Recently, we have collected some questions 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 did a few days of debugging based on previous problems, 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 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. For the controller, I only give examples of adding and searching, so the code is as follows:

Copy code 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 submission 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 added successfully');
} 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 like this, because it has always been written like this because of .NET. To instantiate a specific model, it is strictly required that I operate on the User table.

The code for obtaining POST data will not be explained much.

$M_User->create();

This is a method of ThinkPHP. It is very good and can help you filter out illegal things. It is recommended to use it.

$Query_Result = $M_User->add($data);

This section is about adding data. I am used to specifying the data to be added because this section needs to be instantiated according to $M_User and filter fields. Of course, as long as we code the MODEL well, there will be no problem. The following code will not be explained. There are official documents.

My Model is like this: UserModel.class.php

protected $fields = array( 'id', 'username', 'password' );
Copy after login

Model explanation: This is the key point. This way, the mapping field array of $M_User generated by new will not be empty. Only in this way can it correspond to the POST data, so that the filtering method can recognize it normally and not be filtered. .

6. After the above operations, the database operation for Oracle is completed. I can now use the methods provided by ThinkPHP to operate data, including paging (limit), find(), findAll, etc.

Source http://www.cnblogs.com/aceliu/

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/325643.htmlTechArticle1. 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/ ThinkPH...
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

MySQL: An Introduction to the World's Most Popular Database MySQL: An Introduction to the World's Most Popular Database Apr 12, 2025 am 12:18 AM

MySQL is an open source relational database management system, mainly used to store and retrieve data quickly and reliably. Its working principle includes client requests, query resolution, execution of queries and return results. Examples of usage include creating tables, inserting and querying data, and advanced features such as JOIN operations. Common errors involve SQL syntax, data types, and permissions, and optimization suggestions include the use of indexes, optimized queries, and partitioning of tables.

What to do if the oracle can't be opened What to do if the oracle can't be opened Apr 11, 2025 pm 10:06 PM

Solutions to Oracle cannot be opened include: 1. Start the database service; 2. Start the listener; 3. Check port conflicts; 4. Set environment variables correctly; 5. Make sure the firewall or antivirus software does not block the connection; 6. Check whether the server is closed; 7. Use RMAN to recover corrupt files; 8. Check whether the TNS service name is correct; 9. Check network connection; 10. Reinstall Oracle software.

Why Use MySQL? Benefits and Advantages Why Use MySQL? Benefits and Advantages Apr 12, 2025 am 12:17 AM

MySQL is chosen for its performance, reliability, ease of use, and community support. 1.MySQL provides efficient data storage and retrieval functions, supporting multiple data types and advanced query operations. 2. Adopt client-server architecture and multiple storage engines to support transaction and query optimization. 3. Easy to use, supports a variety of operating systems and programming languages. 4. Have strong community support and provide rich resources and solutions.

How to solve the problem of closing oracle cursor How to solve the problem of closing oracle cursor Apr 11, 2025 pm 10:18 PM

The method to solve the Oracle cursor closure problem includes: explicitly closing the cursor using the CLOSE statement. Declare the cursor in the FOR UPDATE clause so that it automatically closes after the scope is ended. Declare the cursor in the USING clause so that it automatically closes when the associated PL/SQL variable is closed. Use exception handling to ensure that the cursor is closed in any exception situation. Use the connection pool to automatically close the cursor. Disable automatic submission and delay cursor closing.

How to create cursors in oracle loop How to create cursors in oracle loop Apr 12, 2025 am 06:18 AM

In Oracle, the FOR LOOP loop can create cursors dynamically. The steps are: 1. Define the cursor type; 2. Create the loop; 3. Create the cursor dynamically; 4. Execute the cursor; 5. Close the cursor. Example: A cursor can be created cycle-by-circuit to display the names and salaries of the top 10 employees.

How to stop oracle database How to stop oracle database Apr 12, 2025 am 06:12 AM

To stop an Oracle database, perform the following steps: 1. Connect to the database; 2. Shutdown immediately; 3. Shutdown abort completely.

Oracle's Role in the Business World Oracle's Role in the Business World Apr 23, 2025 am 12:01 AM

Oracle is not only a database company, but also a leader in cloud computing and ERP systems. 1. Oracle provides comprehensive solutions from database to cloud services and ERP systems. 2. OracleCloud challenges AWS and Azure, providing IaaS, PaaS and SaaS services. 3. Oracle's ERP systems such as E-BusinessSuite and FusionApplications help enterprises optimize operations.

What to do if the oracle log is full What to do if the oracle log is full Apr 12, 2025 am 06:09 AM

When Oracle log files are full, the following solutions can be adopted: 1) Clean old log files; 2) Increase the log file size; 3) Increase the log file group; 4) Set up automatic log management; 5) Reinitialize the database. Before implementing any solution, it is recommended to back up the database to prevent data loss.

See all articles