homepage >> PHP code to take Drupal 7's watchdog    
 

Yossef Benharosh is an apt web developer and the author of the eBook The essentials of object oriented PHP.

Yossef Benharosh web developer profile linkedin twitter github

PHP code to take Drupal 7's watchdog records and turn them into meaningful messages

Here I use PHP to process the data after retrieving it from the database by querying Drupal's 7 watchdog table. MySQL itself isn't well-suited for decoding and mapping a serialized array like the one in the variables column. Here's how the task can achieved by using PHP:

Step-by-Step Solution

  1. Retrieve Data: Query the database, ordering by severity ascending and wid descending.

  2. Process the variables Blob: Deserialize the variables column to extract its key-value pairs into an associative array.

  3. Replace Placeholders in message: Use PHP's str_replace or preg_replace_callback to replace the %tokens in message with corresponding values from the deserialized array.

  4. Output the Result: Print the processed messages in the desired order.

PHP code

<?php
// Database connection
$mysqli = new mysqli("localhost", "username", "password", "database");

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

// Query to fetch data, ordered by severity ASC and wid DESC
$sql = "SELECT message, variables FROM your_table ORDER BY severity ASC, wid DESC";
$result = $mysqli->query($sql);

if ($result->num_rows > 0) {
    while ($row = $result->fetch_assoc()) {
        $message = $row['message'];
        $variablesBlob = $row['variables'];
        
        // Convert the serialized blob into an associative array
        $variables = unserialize($variablesBlob);

        // Replace placeholders in the message with actual values
        foreach ($variables as $key => $value) {
            $message = str_replace($key, $value, $message);
        }

        // Output the processed message
        echo "

$message

"; } } else { echo "No results found."; } // Close the database connection $mysqli->close();

Explanation of the Code

  1. Query the Data: The SQL query retrieves the message and variables columns while ordering the results by severity ascending and wid descending.

  2. Deserialize the Variables: The unserialize() function converts the variables column (stored as a serialized blob) into an associative PHP array.

  3. Replace Placeholders: The str_replace() function is used to map %tokens in message to their corresponding values from the variables array.

  4. Display the Result: The processed message is outputted inside a <p> tag for clarity.

Example Output

The output might look like:

Deprecated function: Optional parameter $number declared before required parameter $tooltips is implicitly treated as a required parameter in include_once() (line 1445 of /var/www/html/includes/bootstrap.inc).

This approach is clean, efficient, and uses PHP's strengths to dynamically process the data. Let me know if you'd like further refinements!

 

Recommended for you:

Linux terminal cheat sheet

Upload files to Laravel app

Using jQuery and AJAX to dynamically update a page