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 specific PHP extension for a particular purpose, such as database access.
Applicable versions
OceanBase Cloud of all versions.
Prerequisites
- You have installed PHP and php-mysql.
- You have registered an OceanBase Cloud account and created an instance and a MySQL compatible tenant. For more information, see Create an instance and Create a tenant.
Procedure
- Check whether the PHP and php-mysql environments are installed.
- Create an
obtest.phpsample file and configure the database connection information. - Run the
obtest.phpfile and verify the result.
Step 1: Check whether the PHP and php-mysql environments are installed
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, you can run 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 situation.
Here is an 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 whether the connection is successful
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
echo "Connection succeeded";
?>
where:
$servername: the connection address of OceanBase Cloud.$port: the port for connecting to OceanBase Cloud. The default value is 3306.$username: the account for accessing the database.$password: the password of the account.$dbname: the name of the database.
Step 3: Run the obtest.php file and verify the result
Run the php obtest.php command. The following sample output and failure cause are returned:
[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 you enter the correct password, the connection is successful. The following sample output is returned:
[root]# php obtest.php
Connection succeeded
You can perform operations such as table creation and data insertion, modification, and deletion. Here is an example:
[root]# vim obtest.php
<?php
$servername = "t********.********.oceanbase.cloud"; // The server address.
$port = "3306";
$username = "mysql001"; // The username.
$password = "********"; // The 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 is created successfully.";
} else {
echo "Table creation failed: " . 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 "Data insertion failed: " . mysqli_error($conn);
}
// Close the connection
mysqli_close($conn);
?>
If the execution is successful, the following result is returned:
[root]# php obtest.php
Table myguests is created successfully. New record inserted successfully. id: 1 - Name: John Doe<br>Table myguests is deleted successfully.