In PHP queue workers, how do I store results in a database?

In a PHP queue worker, you might need to store the results of processed tasks into a database. This involves fetching the results from the queue, processing them, and then inserting the results into your database using PDO or MySQLi. Below is an example of how you can achieve this.

<?php // Database connection using PDO $dsn = 'mysql:host=localhost;dbname=your_database;charset=utf8mb4'; $username = 'your_username'; $password = 'your_password'; try { $pdo = new PDO($dsn, $username, $password); $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); } catch (PDOException $e) { echo 'Connection failed: ' . $e->getMessage(); } // Assume $taskResult contains the result of a queued task $taskResult = 'This is the result from the queue worker.'; // Prepared statement to insert the result into the database $stmt = $pdo->prepare("INSERT INTO results_table (result) VALUES (:result)"); $stmt->bindParam(':result', $taskResult); if ($stmt->execute()) { echo 'Result stored successfully!'; } else { echo 'Failed to store the result.'; } ?>

PHP queue workers database PDO MySQLi results store results PHP queue example