This topic introduces how to build an application by using EXT and OceanBase Database. It also covers the use of the application for fundamental database operations, including table creation and data insertion.
EXT is a PHP extension used for specific purposes, such as database access.
Applicable versions
This content is applicable to all versions of OceanBase Database.
Prerequisites
- You have installed php and php-mysql.
- You have installed OceanBase Database and created a tenant of the MySQL mode.
Procedure
- Check and install the php and php-mysql environment.
- Create an
obtest.phpsample file and configure the database connection information. - Run the
obtest.phpfile and perform verification.
Step 1: Check and install the php and php-mysql environment
Execute the following commands to install the PHP environment:
sudo yum install php
sudo yum install php-mysql
Execute the following command to check whether the installation is successful:
[root]# php -v
PHP 5.4.16 (cli) (built: Dec 13 2015 00:05:14)
Copyright (c) 1997-2013 The PHP Group
Zend Engine v2.4.0, Copyright (c) 1998-2013 Zend Technologies
Step 2: Create an obtest.php sample file and configure the database connection information
In the Linux environment, for example, you can use the command vi obtest.php or vim obtest.php to edit the obtest.php file, and modify the database connection information in the file based on your specific needs.
Here is an example:
[root]# vim obtest.php
<?php
$servername = "172.30.xxx.xxx";
$port = "2881";
$username = "root@mysql";
$password = "xxxx";
$dbname = "test";
// Create a connection
$conn = mysqli_connect($servername, $username, $password, $dbname, $port);
// Check whether the connection is successful
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
echo "Connection succeeded";
?>
where:
$servernameindicates the server address.$usernameindicates the username for connecting to the database.$passwordindicates the password for connecting to the database.$dbnameindicates the name of the database to connect to.
Step 3: Run the obtest.php file and perform verification
After you execute php obtest.php, the following information will be returned:
[root]# php obtest.php
PHP Warning: mysqli_connect(): (42000/1045): Access denied for user 'root'@'xxx.xxx.xxx.xxx' (using password: YES) in /home/admin/obtest.php on line 9
Connection failed: Access denied for user 'root'@'xxx.xxx.xxx.xxx' (using password: YES)
Enter the correct password. Then, the following information will be returned:
[root]# php obtest.php
Connection succeeded
Create a table and perform CRUD operations for testing. Here is an example:
[root]# vim obtest.php
<?php
$servername = "172.30.xxx.xxx"; // Server address
$port = "2881";
$username = "root@mysql001"; // Username
$password = "xxxxxxx"; // Password
$dbname = "test"; // Name of the database to connect to
// Create a connection
$conn = mysqli_connect($servername, $username, $password, $dbname, $port);
// Check whether the connection is successful
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
// Create a table
$sql = "CREATE TABLE myguests (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
firstname VARCHAR(30) NOT NULL,
lastname VARCHAR(30) NOT NULL,
email VARCHAR(50),
reg_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
)";
if (mysqli_query($conn, $sql)) {
echo "Table myguests is created successfully";
} else {
echo "Table creation error: " . mysqli_error($conn);
}
// Insert data
$sql = "INSERT INTO myguests (firstname, lastname, email)
VALUES ('John', 'Doe', 'john@example.com')";
if (mysqli_query($conn, $sql)) {
echo "New record is inserted successfully";
} else {
echo "Data insertion error: " . mysqli_error($conn);
}
// Close the connection
mysqli_close($conn);
?>
The following information will be returned if the execution is successful:
[root]# php obtest.php
Table myguests is created successfully. New record is inserted successfully. id: 1 - Name: John Doe<br>Table myguests is deleted successfully.