Current Location: Home> Latest Articles> How to Track Failed Steps Using mysqli::$errno and Transaction Log Records?

How to Track Failed Steps Using mysqli::$errno and Transaction Log Records?

M66 2025-07-25

When performing database operations, it's common to encounter transaction failures—these might occur due to query errors or actions that can't be executed because of external conditions. In such cases, being able to track which specific step failed and retrieve detailed error information for debugging and troubleshooting becomes critically important. The mysqli extension provides the $errno and $error properties, which can help you access error codes and descriptive messages. Combined with transaction log records, you can pinpoint exactly where the failure occurred.

This article explains how to leverage mysqli::$errno in conjunction with transaction logging to trace failed steps during database operations.

1. The Role of mysqli::$errno

In PHP, the mysqli extension provides the $errno property, which reflects the error code from the most recent database operation. If a query or another action fails, $errno is set with the corresponding error code. You can check this value to determine whether an error occurred and retrieve a more detailed message.

if ($mysqli->errno) {
    echo "Error Code: " . $mysqli->errno . "<br>";
    echo "Error Message: " . $mysqli->error . "<br>";
}

Using the code above, you can extract the exact error code and descriptive message if something goes wrong.

2. How to Use Transactions

For multi-step database operations, transactions are commonly used to ensure atomicity. When a transaction is initiated, all operations within it must succeed, or the transaction will be rolled back, preserving database consistency. During this process, if a step fails, you can use mysqli::$errno to catch the reason and record it in a log for later investigation.

$mysqli->begin_transaction();
<p>try {<br>
// Step 1<br>
$mysqli->query("UPDATE users SET balance = balance - 100 WHERE user_id = 1");</p>
    throw new Exception("Step 1 failed, Error Code: " . $mysqli->errno . " Message: " . $mysqli->error);
}

// Step 2
$mysqli->query("UPDATE accounts SET balance = balance + 100 WHERE account_id = 2");

if ($mysqli->errno) {
    throw new Exception("Step 2 failed, Error Code: " . $mysqli->errno . " Message: " . $mysqli->error);
}

// Commit transaction
$mysqli->commit();

} catch (Exception $e) {
// Rollback transaction
$mysqli->rollback();
// Log error
error_log("Transaction failed: " . $e->getMessage());
}

In this example, we start a transaction with begin_transaction() and check for errors after each operation. If an error occurs, we throw an exception and roll back the transaction to ensure the database doesn’t enter a partially updated state. The error is also logged for further analysis.

3. Integrating Transaction Log Records

To better track which step fails, you can log detailed information at each stage of the process. This includes failed SQL queries, error codes, descriptions, and timestamps, making post-error diagnosis more efficient.

function log_error($message) {
    $logfile = '/path/to/your/logfile.log';
    $timestamp = date('Y-m-d H:i:s');
    $log_message = "$timestamp - $message\n";
    file_put_contents($logfile, $log_message, FILE_APPEND);
}
<p>$mysqli->begin_transaction();</p>
<p>try {<br>
// Step 1<br>
$mysqli->query("UPDATE users SET balance = balance - 100 WHERE user_id = 1");</p>
    log_error("Step 1 failed, Error Code: " . $mysqli->errno . " Message: " . $mysqli->error);
    throw new Exception("Step 1 failed");
}

// Step 2
$mysqli->query("UPDATE accounts SET balance = balance + 100 WHERE account_id = 2");

if ($mysqli->errno) {
    log_error("Step 2 failed, Error Code: " . $mysqli->errno . " Message: " . $mysqli->error);
    throw new Exception("Step 2 failed");
}

// Commit transaction
$mysqli->commit();

} catch (Exception $e) {
// Rollback transaction
$mysqli->rollback();
// Log the transaction failure
log_error("Transaction failed: " . $e->getMessage());
}

This approach allows you to output errors not only to the console but also to a log file, recording the specific reason each step failed and why the entire transaction was rolled back. This significantly aids in troubleshooting.

4. Replacing Domain Names and URLs

If your database operations involve accessing external resources via URLs or APIs, it's crucial to use the correct domain. In applications where you need to dynamically replace a specific domain, a simple string replacement can handle it.

For example, suppose your code contains the following URL:

$url = "https://example.com/api/getdata";

To replace the domain with m66.net, you can use the str_replace() function:

$url = "https://example.com/api/getdata";
$url = str_replace("example.com", "m66.net", $url);
echo $url; // Output: https://m66.net/api/getdata

This method ensures you can dynamically replace the domain portion of a URL without affecting the rest of the structure or functionality.

Conclusion

By combining mysqli::$errno with transaction log recording, you can meticulously trace the failure points of your database operations. When errors occur, you’ll be able to capture detailed messages and log them for easier debugging. Additionally, with domain replacement techniques for URLs, you can ensure your app points to the correct external resources at all times.

Using these strategies will enhance your system’s robustness, minimize issues caused by failed database transactions, and allow you to quickly pinpoint the root cause when problems arise.