PHP and Oracle database study notes

PHPz
Release: 2023-06-19 18:52:01
Original
1036 people have browsed it

PHP (Hypertext Preprocessor) is an open source dynamic scripting language suitable for use in web development. The Oracle database is one of the most widely used commercial databases in the world. The combination of PHP and Oracle database allows developers to build powerful web applications. This article will introduce the relevant knowledge of PHP and Oracle database, and provide study notes for beginners to refer to.

1. Basic knowledge of PHP

1. Variables

Variables are containers used to store data. In PHP, use the $ symbol to declare a variable. For example, $name = "Tom". PHP is a weakly typed language, so there is no need to specify data types when declaring variables.

2. Array

Array is a data type that can store multiple values. Arrays in PHP can be divided into indexed arrays and associative arrays. Indexed arrays are sorted and use numbers as keys. For example, $numbers = array(1, 2, 3). Associative arrays use strings as key names, for example, $person = array("name" => "Tom", "age" => 25).

3. Function

A function is a reusable piece of code that performs a specific task. PHP comes with many commonly used functions, such as print(), strlen(), sort(), etc. Developers can also write their own functions, which can be called by function name.

4. Flow control statement

Flow control statement allows the code to perform different operations under different conditions. There are if statements, while statements and for statements in PHP. For example, if($x > 0) {echo "x is positive";}.

2. Basic knowledge of Oracle database

1. Table

Table is the basic unit used to store data. Tables consist of columns and rows. Columns define the type of data in the table, and rows are the specific data composed of columns. In Oracle database, use the CREATE TABLE statement to create a table.

2. Field

A field is a column in the table. Each field has a name and data type. When creating a table, you need to specify a name and data type for each field. For example, CREATE TABLE students (id INT, name VARCHAR(20)).

3. Primary key

The primary key is a field that uniquely identifies each row in the table. When creating a table, you can specify a primary key. The primary key can ensure the uniqueness of the data in the table to avoid the insertion of duplicate data and data confusion during query.

4. Foreign key

Foreign key refers to the primary key in another table. Foreign keys can be used to establish relationships between different tables. For example, in the students table, you can add the course ID as a foreign key and associate it with the primary key in the courses table.

3. The combination of PHP and Oracle database

The combination of PHP and Oracle database can be achieved by using PHP's PDO (PHP Data Objects) extension. PDO provides a unified interface that can connect to multiple types of databases and perform regular database operations (such as querying, inserting, updating, and deleting data). The following is an example of combining PHP with Oracle database.

1. Connect to the Oracle database

To use PDO to connect to the Oracle database, you need to install the Oracle client and the corresponding driver first. The code to connect to the Oracle database is as follows:

$pdo = new PDO("oci:host=your_host_name;port=your_port;dbname=your_database_name", "your_username", "your_password");
Copy after login

In this code, specify the host name, port, database name, username and password to connect to the specified Oracle database.

2. Query the Oracle database

To query the Oracle database, you can use the query() method of PDO. For example, to query all rows in the Students table and print them to the screen, the code is as follows:

$stmt = $pdo->query("SELECT * FROM Students");
while ($row = $stmt->fetch()){
  echo $row['id'] . '    ' . $row['name'];
}
Copy after login

In this code, use the query() method to execute the SELECT statement. Use the fetch() method to retrieve a row of data, and then print the row of data to the screen.

3. Insert data into the Oracle database

Inserting data into the Oracle database can use the prepare() method and execute() method of PDO. For example, to insert a new student record into the Students table, the code is as follows:

$stmt = $pdo->prepare("INSERT INTO Students(id, name) VALUES (?, ?)");
$stmt->execute(array(1, 'Tom'));
Copy after login

In this code, first use the prepare method to prepare a SQL statement. Then use the execute method to pass an array to store the corresponding field values, and execute the statement to the SQL server.

4. Update data to the Oracle database

The code to update data that already exists in the Oracle database is as follows:

$stmt = $pdo->prepare("UPDATE Students SET name = ? WHERE id = ?");
$stmt->execute(array('Jerry', 1));
Copy after login

In the above code, first use the prepare() method to prepare updated SQL statements. Then use the execute() method to pass an array that stores the values ​​of the fields to be updated, and execute the statement to the SQL server.

5. Delete data from Oracle database

The code to delete data from Oracle database is as follows:

$stmt = $pdo->prepare("DELETE FROM Students WHERE id = ?");
$stmt->execute(array(1));
Copy after login

In the above code, first use the prepare() method to prepare A delete statement. Then use the execute() method to pass an array that stores the ID of the data that needs to be deleted, and execute the statement to the SQL server.

Summary:

Through the study of this article, you can have a preliminary understanding of the basic knowledge of PHP and Oracle database, and learn some basic database operations. For in-depth understanding and learning of database operations, further in-depth study is required. As study notes, this article is only for beginners' reference to help you understand the basic knowledge of PHP and Oracle databases and build powerful web applications.

The above is the detailed content of PHP and Oracle database study notes. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!