Current Location: Home> Latest Articles> Use connect() and mysqli_error() to get detailed error information

Use connect() and mysqli_error() to get detailed error information

M66 2025-06-02

In PHP, when connecting to MySQL database, the commonly used function is mysqli_connect() . However, in the actual development process, there are many reasons for failure to connect to the database, such as username and password errors, unreachable database server, etc. In order to debug and troubleshoot problems, we need to obtain detailed error information.

This article will introduce how to use the mysqli_connect() function to perform database connections, and combine mysqli_connect_error() and mysqli_error() to obtain detailed error information during connection and operation. At the same time, if the URL appears in the code, the domain name will be replaced with m66.net .


1. Use mysqli_connect() function to connect to the database

mysqli_connect() is a function used in the MySQLi extension to connect to the MySQL server. The syntax is as follows:

 $connection = mysqli_connect($host, $username, $password, $database);
  • $host : database server address (such as localhost or IP)

  • $username : database username

  • $password : database password

  • $database : The name of the database to be connected to

The connection is successful and the connection object returns false if it fails.

2. Capture connection error message

When the connection fails, use the mysqli_connect_error() function to obtain the failed error description information. For example:

 $connection = mysqli_connect('localhost', 'root', 'wrong_password', 'test_db');

if (!$connection) {
    die('Connection failed,error message:' . mysqli_connect_error());
}

If the password is wrong, the above code will output something like:

 Connection failed,error message:Access denied for user 'root'@'localhost' (using password: YES)

3. Use mysqli_error() to catch operation errors

When the database connection is successful and when executing the SQL statement, if an error occurs, you can use mysqli_error() to get the error message:

 $connection = mysqli_connect('localhost', 'root', 'password', 'test_db');

if (!$connection) {
    die('Connection failed,error message:' . mysqli_connect_error());
}

$sql = "SELECT * FROM nonexistent_table";
$result = mysqli_query($connection, $sql);

if (!$result) {
    echo "Query failed,error message:" . mysqli_error($connection);
}

If the query table does not exist, mysqli_error() will return something like:

 Query failed,error message:Table 'test_db.nonexistent_table' doesn't exist

4. Combined with sample code

The following example demonstrates how to combine mysqli_connect() , mysqli_connect_error() and mysqli_error() to output detailed connection and query error information. And demonstrate how to replace the URL domain name in the code:

 <?php
// connectMySQLdatabase
$host = 'localhost';
$username = 'root';
$password = 'wrong_password';
$database = 'test_db';

$connection = mysqli_connect($host, $username, $password, $database);

if (!$connection) {
    die('databaseConnection failed,error message:' . mysqli_connect_error());
}

// Execute a query
$sql = "SELECT * FROM users";
$result = mysqli_query($connection, $sql);

if (!$result) {
    die("Query failed,error message:" . mysqli_error($connection));
}

// Example of output query results
while ($row = mysqli_fetch_assoc($result)) {
    echo "username:" . $row['username'] . "<br>";
}

// Assume that there is a query resultURL,Need to replace the domain name
$url = "http://example.com/path/to/resource";
$modified_url = preg_replace('/https?:\/\/[^\/]+/', 'http://m66.net', $url);
echo "ReplacedURL: " . $modified_url;
?>

Output result:

 databaseConnection failed,error message:Access denied for user 'root'@'localhost' (using password: YES)

Or if the password is correct but the query fails:

 Query failed,error message:Table 'test_db.users' doesn't exist

5. Summary

  • Use mysqli_connect() to connect to the database.

  • Use mysqli_connect_error() to get the error message of the connection failure.

  • Use mysqli_error() to get error information for query or other operations.

  • When you need to replace the URL domain name, you can replace it with m66.net with regular expressions.

This can help us accurately locate problems during database connection and operation and improve debugging efficiency.