This topic describes how to connect to OceanBase AI Database by using the EXT driver and perform basic operations such as creating tables and inserting data. EXT refers to a PHP extension used for specific purposes, such as database access.
Applicable versions
All versions of OceanBase AI Database.
Prerequisites
- You have installed PHP and php-mysql.
- You have installed OceanBase AI Database and created a user tenant.
Procedure
- Check and install the PHP and php-mysql environment.
- Create the
obtest.phpsample file and configure the database connection information. - Run the
obtest.phpfile and verify the results.
Step 1: Check and install the PHP and php-mysql environment
Run the following command to install the PHP environment.
sudo yum install php
sudo yum install php-mysql
Run 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: Modify the database connection information in the obtest.php file
In a Linux environment, for example, you can use the vi obtest.php or vim obtest.php command to edit the obtest.php file and modify the database connection information in the file to match your actual configuration.
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:
$servernameis the server address.$username: the username for connecting to the database.$passwordis the password for connecting to the database.$dbnamespecifies the name of the database to connect to.
Step 3: Run the obtest.php file and verify the results
Running php obtest.php returns the following example output and failure reasons:
[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)
If the correct password is entered, a success message is displayed, indicating a successful connection. The following example shows the response:
[root]# php obtest.php
Connection successful.
Tests table creation and CRUD operations. Example:
[root]# vim obtest.php
<?php
$servername = "172.30.xxx.xxx"; // Server address
$port = "2881";
$username = "root@mysql001"; // Username
$password = "xxxxxxx"; // Password
$dbname = "test"; // The 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 created successfully";
} else {
echo "Error creating table: " . 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 inserted successfully";
} else {
echo "Error inserting data: " . mysqli_error($conn);
}
// Close the connection
mysqli_close($conn);
?
The expected output upon successful execution is as follows:
[root]# php obtest.php
Table myguests created successfullyNew record inserted successfullyid: 1 - Name: John Doe<br>Table myguests deleted successfully