In PHP web development, how do I store results in a database?

In PHP web development, you can store results in a database using the PDO (PHP Data Objects) extension or MySQLi. Below is an example demonstrating how to insert data into a MySQL database using PDO:

<?php // Database connection parameters $host = 'localhost'; $dbname = 'my_database'; $username = 'my_username'; $password = 'my_password'; try { // Create a new PDO instance $pdo = new PDO("mysql:host=$host;dbname=$dbname", $username, $password); $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // Prepare an SQL statement for execution $stmt = $pdo->prepare("INSERT INTO my_table (name, email) VALUES (:name, :email)"); // Bind parameters to the SQL query $stmt->bindParam(':name', $name); $stmt->bindParam(':email', $email); // Values to insert $name = 'John Doe'; $email = 'john.doe@example.com'; // Execute the statement $stmt->execute(); echo "New record created successfully"; } catch (PDOException $e) { echo "Error: " . $e->getMessage(); } // Close the database connection $pdo = null; ?>

PHP MySQL Database PDO Web Development