OceanBase logo

OceanBase

A unified distributed database ready for your transactional, analytical, and AI workloads.

Product Overview
DEPLOY YOUR WAY

OceanBase Cloud

The best way to deploy and scale OceanBase

OceanBase Enterprise

Run and manage OceanBase on your infra

TRY OPEN SOURCE

OceanBase Community Edition

The free, open-source distributed database

OceanBase seekdb

Open source AI native search database

Customer Stories

Real-world success stories from enterprises across diverse industries.

View All
BY USE CASES

Mission-Critical Transactions

Global & Multicloud Application

Elastic Scaling for Peak Traffic

Real-time Analytics

Active Geo-redundancy

Database Consolidation

Resources

Comprehensive knowledge hub for OceanBase.

Blog

Live Demos

Training & Certification

Documentation

Official technical guides, tutorials, API references, and manuals for all OceanBase products.

View All
PRODUCTS

OceanBase Cloud

OceanBase Database

Tools

Connectors and Middleware

QUICK START

OceanBase Cloud

OceanBase Database

BEST PRACTICES

Practical guides for utilizing OceanBase more effectively and conveniently

Company

Learn more about OceanBase – our company, partnerships, and trust and security initiatives.

About OceanBase

Partner

Trust Center

Contact Us

International - English
中国站 - 简体中文
日本 - 日本語
Sign In
Start on Cloud

OceanBase

A unified distributed database ready for your transactional, analytical, and AI workloads.

Product Overview
DEPLOY YOUR WAY

OceanBase Cloud

The best way to deploy and scale OceanBase

OceanBase Enterprise

Run and manage OceanBase on your infra

TRY OPEN SOURCE

OceanBase Community Edition

The free, open-source distributed database

OceanBase seekdb

Open source AI native search database

Customer Stories

Real-world success stories from enterprises across diverse industries.

View All
BY USE CASES

Mission-Critical Transactions

Global & Multicloud Application

Elastic Scaling for Peak Traffic

Real-time Analytics

Active Geo-redundancy

Database Consolidation

Comprehensive knowledge hub for OceanBase.

Blog

Live Demos

Training & Certification

Documentation

Official technical guides, tutorials, API references, and manuals for all OceanBase products.

View All
PRODUCTS
OceanBase CloudOceanBase Database
ToolsConnectors and Middleware
QUICK START
OceanBase CloudOceanBase Database
BEST PRACTICES

Practical guides for utilizing OceanBase more effectively and conveniently

Learn more about OceanBase – our company, partnerships, and trust and security initiatives.

About OceanBase

Partner

Trust Center

Contact Us

Start on Cloud
编组
All Products
    • Databases
    • iconOceanBase Database
    • iconOceanBase Cloud
    • iconOceanBase Tugraph
    • iconInteractive Tutorials
    • iconOceanBase Best Practices
    • Tools
    • iconOceanBase Cloud Platform
    • iconOceanBase Migration Service
    • iconOceanBase Developer Center
    • iconOceanBase Migration Assessment
    • iconOceanBase Admin Tool
    • iconOceanBase Loader and Dumper
    • iconOceanBase Deployer
    • iconKubernetes operator for OceanBase
    • iconOceanBase Diagnostic Tool
    • iconOceanBase Binlog Service
    • Connectors and Middleware
    • iconOceanBase Database Proxy
    • iconEmbedded SQL in C for OceanBase
    • iconOceanBase Call Interface
    • iconOceanBase Connector/C
    • iconOceanBase Connector/J
    • iconOceanBase Connector/ODBC
    • iconOceanBase Connector/NET
icon

OceanBase Database

SQL - V4.4.2

    Download PDF

    OceanBase logo

    The Unified Distributed Database for the AI Era.

    Follow Us
    Products
    OceanBase CloudOceanBase EnterpriseOceanBase Community EditionOceanBase seekdb
    Resources
    DocsBlogLive DemosTraining & CertificationTicket
    Company
    About OceanBaseTrust CenterLegalPartnerContact Us
    Follow Us

    © OceanBase 2026. All rights reserved

    Cloud Service AgreementPrivacy PolicySecurity
    Contact Us
    Document Feedback
    1. Documentation Center
    2. OceanBase Database
    3. SQL
    4. V4.4.2
    iconOceanBase Database
    SQL - V 4.4.2
    SQL
    KV
    • V 4.6.0
    • V 4.4.2
    • V 4.3.5
    • V 4.3.3
    • V 4.3.1
    • V 4.3.0
    • V 4.2.5
    • V 4.2.2
    • V 4.2.1
    • V 4.2.0
    • V 4.1.0
    • V 4.0.0
    • V 3.1.4 and earlier

    Connect to OceanBase Database by using the PDO driver

    Last Updated:2026-04-02 06:23:56  Updated
    share
    What is on this page
    Applicable versions
    Prerequisites
    Procedure
    Step 1: Check and install the php and php-mysql environment
    Step 2: Create a podtest.php sample file and configure the database connection information
    Step 3: Run the podtest.php file and perform verification

    folded

    share

    This topic introduces how to build an application by using PDO and OceanBase Database. It also covers the use of the application for fundamental database operations, including table creation and data insertion.

    PDO, short for PHP Data Objects, is a lightweight, consistent interface used in PHP applications to provide a data access abstraction layer.

    Applicable versions

    This content is applicable to all versions of OceanBase Database.

    Prerequisites

    • You have installed php and php-mysql.
    • You have installed OceanBase Database and created a tenant of the MySQL-compatible mode.

    Procedure

    1. Check and install the php and php-mysql environment.
    2. Create a podtest.php sample file and configure the database connection information.
    3. Run the podtest.php file and perform verification.

    Step 1: Check and install the php and php-mysql environment

    For specific instructions on how to install the php and php-mysql environment, see Step 1: Check and install the php and php-mysql environment in Connect to OceanBase Database by using an EXT driver.

    Step 2: Create a podtest.php sample file and configure the database connection information

    In the Linux environment, for example, you can use the command vi podtest.php or vim podtest.php to edit the podtest.php file, and modify the database connection information in the file based on your specific needs.

    Here is an example:

    [root]# vim podtest.php
    <?php
    $servername = "172.30.xx.xxx";
    $port = "2881";
    $username = "root@mysql";
    $password = "xxxxxxx";
    $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 succeeded";
    } catch(PDOException $e) {
        echo "Connection failed: " . $e->getMessage();
    }
    ?>
    

    where

    • $servername indicates the IP address of the MySQL server.
    • $port indicates the port number of the MySQL server.
    • $username indicates the MySQL username for connecting to the database.
    • $password indicates the MySQL password for connecting to the database.
    • $dbname indicates the name of the database to connect to.

    Step 3: Run the podtest.php file and perform verification

    If you provide an incorrect database name in the podtest.php file, the following error message will be reported when you execute the php podtest.php command:

    [root]# php podtest.php
    Connection failed: SQLSTATE[42000] [1049] Unknown database 'test123'
    

    If the information is correct, the following information will be displayed, indicating that the connection is successful:

    [root]# php podtest.php
    Connection succeeded
    

    Create a table and perform CRUD operations for testing. Here is an example:

    [root]# vim podtest.php
    <?php
    $servername = "172.30.xxx.xxx"; // IP address of the MySQL server
    $port = "2881"; // Port number of the MySQL server
    $username = "root@mysql"; // MySQL username
    $password = ""; // MySQL password
    $dbname = "test"; // Name of the database to connect 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 succeeded";
    } 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 is created successfully";
    } catch(PDOException $e) {
        echo "Table creation error: " . $e->getMessage();
    }
    
    // Insert data
    try {
        $sql = "INSERT INTO myguests (firstname, lastname, email)
        VALUES ('John', 'Doe', 'john@example.com')";
        $conn->exec($sql);
        echo "New record is inserted successfully";
    } catch(PDOException $e) {
        echo "Data insertion error: " . $e->getMessage();
    }
    
    // Query 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 result";
        }
    } catch(PDOException $e) {
        echo "Table query error: " . $e->getMessage();
    }
    
    // Delete table
    try {
        $sql = "DROP TABLE myguests";
        $conn->exec($sql);
        echo "Table myguests is deleted successfully";
    } catch(PDOException $e) {
        echo "Table deletion error: " . $e->getMessage();
    }
    
    // Close the connection
    $conn = null;
    ?>
    

    The following information will be returned if the execution is successful:

    [root]# php podtest.php
    Connection succeeded. Table myguests is created successfully. New record is inserted successfully. id: 1 - Name: John Doe<br>Table myguests is deleted successfully.
    

    Previous topic

    MySQLi
    Last

    Next topic

    Connect to OceanBase Database by using Rust
    Next
    What is on this page
    Applicable versions
    Prerequisites
    Procedure
    Step 1: Check and install the php and php-mysql environment
    Step 2: Create a podtest.php sample file and configure the database connection information
    Step 3: Run the podtest.php file and perform verification