Use the MySQLi driver to connect to seekdb
This topic describes how to use the MySQLi driver to connect to seekdb and perform basic operations such as creating a table and inserting data. MySQLi is an extension for the PHP language that provides both procedural and object-oriented database access methods.
Prerequisites
- You have installed php and php-mysql.
- You have installed seekdb.
Procedure
- Check and install the php and php-mysql environments.
- Create an
obtest1.phpsample file and configure the database connection information. - Run the
obtest1.phpfile and verify the results.
Step 1: Check and install the php and php-mysql environments
Install the PHP and php-mysql environments. For more information, see Connect to seekdb by using the Ext driver Step 1: Check and install the php and php-mysql environments.
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 = "11.158.xx.xx";
$port = "2881";
$username = "root";
$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 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:
$servernamespecifies the address of the seekdb server.$portspecifies the port number of the seekdb server.$usernamespecifies the username of the seekdb user.$passwordspecifies the password for connecting to seekdb.$dbnamespecifies the name of the database to connect to.
Step 3: Run the obtest1.php file and verify the results
Run the obtest1.php file by executing the php obtest1.php command to verify the connection to seekdb and check whether the basic operations such as creating a table 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