This topic describes how to use the EXT driver to connect to OceanBase Cloud 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 Cloud.
Prerequisites
- You have installed PHP and php-mysql.
- You have registered an OceanBase Cloud account and created a cluster instance and a tenant in MySQL mode. For more information, see Create a cluster instance and Create a tenant.
Procedure
- Check and install the PHP and php-mysql environment.
- Create a sample file named
obtest.phpand 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 if the installation was 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 to match your actual configuration.
Example:
[root]# vim obtest.php
<?php
$servername = "t********.********.oceanbase.cloud";
$port = "3306";
$username = "mysql001";
$password = "********";
$dbname = "test";
// Create a connection
$conn = mysqli_connect($servername, $username, $password, $dbname, $port);
// Check if the connection was successful.
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
echo "Connection succeeded";
?>
Where:
$servername: The connection address of OceanBase Cloud.$port: The connection port of OceanBase Cloud, which is 3306 by default.$username: The account used to access the database.$password: The password of the account.$dbname: The name of the database.
Step 3: Run the obtest.php file and verify the results
Running php obtest.php returns an example output and failure reasons as follows:
[root]# php obtest.php
PHP Warning: mysqli_connect(): (42000/1045): Access denied for user 'mysql001'@'t********.********.oceanbase.cloud' (using password: YES) in /home/admin/obtest.php on line 9
Connection failed: Access denied for user 'mysql001'@'t********.********.oceanbase.cloud' (using password: YES)
If the correct password is entered, a success message is returned:
[root]# php obtest.php
Connection succeeded
Test table creation and CRUD operations. An example test is as follows:
[root]# vim obtest.php
<?php
$servername = "t********.********.oceanbase.cloud"; // Server address
$port = "3306";
$username = "mysql001"; // Username
$password = "********"; // Password
$dbname = "test"; // Name of the database to connect to
// Create a connection.
$conn = mysqli_connect($servername, $username, $password, $dbname, $port);
// Check if the connection was 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