This topic describes how to use the PDO driver to connect to OceanBase Cloud and perform basic operations such as creating tables and inserting data. PDO stands for PHP Data Objects. It is a lightweight and consistent interface for providing a data access abstraction layer in PHP applications.
Applicable versions
All versions of OceanBase Cloud.
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 and install the php and php-mysql environments.
- Create a
podtest.phpfile and configure the database connection information. - Run the
podtest.phpfile and verify the connection.
Step 1: Check and install the php and php-mysql environments
Install the PHP and php-mysql environments. For more information, see Step 1: Check and install the php and php-mysql environments in Connect to OceanBase Cloud by using the Ext driver.
Step 2: Modify the database connection information in the podtest.php file
Take Linux as an example. Run the vi podtest.php or vim podtest.php command to edit the podtest.php file and modify the database connection information in the file to match your actual situation.
[root]# vim podtest.php
<?php
$servername = "t********.********.oceanbase.cloud";
$port = "3306";
$username = "mysql001";
$password = "********";
$dbname = "test";
// Create a connection
try {
$conn = new PDO("mysql:host=$servername;port=$port;dbname=$dbname", $username, $password);
// Set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "Connection successful";
} catch(PDOException $e) {
echo "Connection failed: " . $e->getMessage();
}
?>
where:
$servername: the connection address of OceanBase Cloud.$port: the connection port of 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 podtest.php file and verify the connection
In the podtest.php file, intentionally enter an incorrect database name. When you run the php podtest.php command, the following error message is returned:
[root]# php podtest.php
Connection failed: SQLSTATE[42000] [1049] Unknown database 'test123'
After you enter the correct information, the system displays the following message:
[root]# php podtest.php
Connection successful
You can test the table creation and data insertion, deletion, modification, and query operations. Here is an example:
[root]# vim podtest.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 be connected to.
// Create a connection
try {
$conn = new PDO("mysql:host=$servername;port=$port;dbname=$dbname", $username, $password);
// Set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "Connection successful";
} catch(PDOException $e) {
echo "Connection failed: " . $e->getMessage();
}
// Create a table
try {
$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
)";
$conn->exec($sql);
echo "Table myguests created successfully";
} catch(PDOException $e) {
echo "Table creation failed: " . $e->getMessage();
}
// Insert data
try {
$sql = "INSERT INTO myguests (firstname, lastname, email)
VALUES ('John', 'Doe', 'john@example.com')";
$conn->exec($sql);
echo "New record inserted successfully";
} catch(PDOException $e) {
echo "Data insertion failed: " . $e->getMessage();
}
// Query the table
try {
$sql = "SELECT id, firstname, lastname FROM myguests";
$stmt = $conn->prepare($sql);
$stmt->execute();
$result = $stmt->setFetchMode(PDO::FETCH_ASSOC);
if ($stmt->rowCount() > 0) {
// Output data
while($row = $stmt->fetch()) {
echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "<br>";
}
} else {
echo "0 results";
}
} catch(PDOException $e) {
echo "Table query failed: " . $e->getMessage();
}
// Drop the table
try {
$sql = "DROP TABLE myguests";
$conn->exec($sql);
echo "Table myguests dropped successfully";
} catch(PDOException $e) {
echo "Table drop failed: " . $e->getMessage();
}
// Close the connection
$conn = null;
?>
The execution result is as follows:
[root]# php podtest.php
Connection successfulTable myguests created successfullyNew record inserted successfullyid: 1 - Name: John Doe<br>Table myguests dropped successfully