Skip to main content

EXT driver connects to seekdb

This topic describes how to use the EXT driver to connect to seekdb and perform basic operations such as creating a table and inserting data. EXT is a PHP extension for a specific purpose, such as database access.

Prerequisites

  • You have installed PHP and the PHP MySQL extension.
  • You have installed seekdb.

Procedure

  1. Check whether the PHP and PHP MySQL environment is installed.
  2. Create an obtest.php sample file and configure the database connection information.
  3. Run the obtest.php file and verify the result.

Step 1: Check whether the PHP and PHP MySQL environment is 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 PHP environment is installed.

[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: Configure 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 configure the database connection information in the file.

Here is an example:

[root]# vim obtest.php
<?php
$servername = "172.30.xxx.xxx";
$port = "2881";
$username = "root";
$password = "xxxx";
$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 specifies the server address.
  • $username specifies the username for connecting to the database.
  • $password specifies the password for connecting to the database.
  • $dbname specifies the name of the database to connect to.

Step 3: Run the obtest.php file and verify the result

Run the php obtest.php command. If the command fails, the following message is returned:

[root]# php obtest.php
PHP Warning: mysqli_connect(): (42000/1045): Access denied for user 'root'@'xxx.xxx.xxx.xxx' (using password: YES) in /home/admin/obtest.php on line 9
Connection failed: Access denied for user 'root'@'xxx.xxx.xxx.xxx' (using password: YES)

If you enter the correct password, the following message is returned:

[root]# php obtest.php
Connection succeeded

You can test table creation and data insertion, modification, and deletion. Here is an example:

[root]# vim obtest.php
<?php
$servername = "172.30.xxx.xxx"; // Server address
$port = "2881";
$username = "root"; // Username
$password = "xxxxxxx"; // Password
$dbname = "test"; // Database name

// 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 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 command is executed successfully, the following result is returned:

[root]# php obtest.php
Table myguests created successfullyNew record inserted successfullyid: 1 - Name: John Doe<br>Table myguests deleted successfully