This topic describes how to use the MySQLi driver to connect to OceanBase Cloud and perform basic operations such as creating tables and inserting data. MySQLi is a PHP extension that provides both procedural and object-oriented database access methods.
Applicable versions
All versions of OceanBase Cloud.
Prerequisites
- You have installed PHP and the php-mysql extension.
- 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 and install the PHP and php-mysql environment.
- Create an
obtest1.phpsample file and configure the database connection information. - Run the
obtest1.phpfile and verify the connection.
Step 1: Check and install the PHP and php-mysql environment
Install PHP and the php-mysql extension. For more information, see Step 1: Check and install the PHP and php-mysql environment in Connect to OceanBase Cloud by using the Ext driver.
Step 2: Modify the database connection information in the obtest1.php file
In a Linux environment, you can use the vi obtest1.php or vim obtest1.php command to edit the obtest1.php file and modify the database connection information to match your actual setup.
Here is an example:
[root]# vim obtest1.php
<?php
$servername = "t********.********.oceanbase.cloud";
$port = "3306";
$username = "mysql001";
$password = "********";
$dbname = "test";
// Create a connection
$conn = new mysqli($servername, $username, $password, $dbname, $port);
// Check if the connection is successful
if ($conn->connect_error) {
die("Connection failed: " . $conn->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 ($conn->query($sql) === TRUE) {
echo "Table myguests created successfully";
} else {
echo "Error creating table: " . $conn->error;
}
// Insert data
$sql = "INSERT INTO myguests (firstname, lastname, email)
VALUES ('John', 'Doe', 'john@example.com')";
if ($conn->query($sql) === TRUE) {
echo "New record inserted successfully";
} else {
echo "Error inserting data: " . $conn->error;
}
// Query the table
$sql = "SELECT id, firstname, lastname FROM MyGuests";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// Output the data
while($row = $result->fetch_assoc()) {
echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "<br>";
}
} else {
echo "0 results";
}
// Drop the table
$sql = "DROP TABLE myguests";
if ($conn->query($sql) === TRUE) {
echo "Table myguests dropped successfully";
} else {
echo "Error dropping table: " . $conn->error;
}
// Close the connection
$conn->close();
?>
Where:
$servername: the connection address of OceanBase Cloud.$port: the connection port of OceanBase Cloud, which is 3306 by default.$username: the account for accessing the database.$password: the password of the account.$dbname: the name of the database.
Step 3: Run the obtest1.php file and verify the connection
Run the obtest1.php file by executing the php obtest1.php command to verify whether the basic operations such as creating tables and inserting data are successful.
Here is an example:
[root]# php obtest1.php
Table myguests created successfullyNew record inserted successfullyid: 1 - Name: John Doe<br>Table myguests dropped successfully