Current Location: Home> Latest Articles> Write a general database debugging tool to display connection information using mysqli::get_charset

Write a general database debugging tool to display connection information using mysqli::get_charset

M66 2025-05-22

In PHP, database debugging tools can help developers easily view and manage connection status, query execution status, character set and other information with the database. This article will use an example to show how to write a common database debugging tool and use mysqli::get_charset to display the character set information of the currently connected.

1. Database connection

First, we need a database connection object to operate the database. When using the mysqli extension, you can create a connection with the following code:

 <?php
// Database connection configuration
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "test_db";

// Create a connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Check the connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}
?>

Here we create a connection object $conn that connects to the MySQL database test_db . If the connection fails, an error message will be output and the script will be terminated.

2. Create a common database debugging tool

Next, we will write a general database debugging tool class, which can perform simple queries, view connection information, view character sets, etc.

 <?php
class DbDebugger {
    private $conn;

    public function __construct($connection) {
        $this->conn = $connection;
    }

    // Execute the query and return the result
    public function executeQuery($sql) {
        $result = $this->conn->query($sql);
        if ($result === false) {
            return "Query error: " . $this->conn->error;
        }
        
        $rows = [];
        while ($row = $result->fetch_assoc()) {
            $rows[] = $row;
        }
        return $rows;
    }

    // Get the character set information of the current connection
    public function getCharsetInfo() {
        return $this->conn->get_charset();
    }

    // Get current connection information
    public function getConnectionInfo() {
        return [
            'host' => $this->conn->host_info,
            'client_version' => $this->conn->client_version,
            'server_version' => $this->conn->server_version,
        ];
    }
}
?>

explain:

  • The DbDebugger class accepts a MySQLi connection object as a parameter to the constructor.

  • The executeQuery method executes an SQL query and returns the query result. If the query fails, an error message will be returned.

  • The getCharsetInfo method obtains the character set information of the current connection through mysqli::get_charset .

  • The getConnectionInfo method returns some basic information about the database connection, such as host information, client version and server version.

3. Use debugging tools

Now, we have created a common database debugging tool class. Next, demonstrate how to use this tool class to query database information and view character sets.

 <?php
// Includes debugging tool classes
include 'DbDebugger.php';

// Create a database connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Instantiated database debugging tool
$dbDebugger = new DbDebugger($conn);

// Execute the query and output the result
$sql = "SELECT * FROM users";
$queryResult = $dbDebugger->executeQuery($sql);
echo "<pre>";
print_r($queryResult);
echo "</pre>";

// Output the character set information of the currently connected
echo "Current connection character set: " . $dbDebugger->getCharsetInfo()['charset'] . "<br>";

// Output connection information
$connectionInfo = $dbDebugger->getConnectionInfo();
echo "Host information: " . $connectionInfo['host'] . "<br>";
echo "Client version: " . $connectionInfo['client_version'] . "<br>";
echo "Server version: " . $connectionInfo['server_version'] . "<br>";
?>

explain:

  • First we instantiate the DbDebugger class and pass in the database connection object $conn .

  • The executeQuery method is called to execute SQL query and output the result.

  • Use getCharsetInfo to get and display the character set information of the currently connected.

  • Use getConnectionInfo to output the connection's host information, client version, and server version.

4. Display character set information

mysqli::get_charset returns an associative array containing the character set information of the current connection, for example:

 Array
(
    [charset] => utf8mb4
    [collation] => utf8mb4_general_ci
)

We can obtain the information of the current character set through the charset key, such as UTF-8, UTF-8MB4, etc. Character sets are very important for the storage and reading of databases, ensuring that the application can process character data correctly.

Summarize

This article shows how to write a common database debugging tool using PHP. Through the mysqli::get_charset method, we can easily obtain the character set information of the current database connection. In addition, with the help of this tool, we can also query the database, obtain connection information, etc. This tool is very helpful for debugging database connections, query execution and other operations during development.