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

Storing results in a database using PHP involves connecting to the database, preparing a SQL statement, binding parameters, and executing the statement. Below is an example of how to save data from a form into a MySQL database using PHP.

<?php // Database connection parameters $servername = "localhost"; $username = "your_username"; $password = "your_password"; $dbname = "your_database"; // Create connection $conn = new mysqli($servername, $username, $password, $dbname); // Check connection if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } // Prepare and bind $stmt = $conn->prepare("INSERT INTO results (name, score) VALUES (?, ?)"); $stmt->bind_param("si", $name, $score); // Set parameters and execute $name = "John Doe"; $score = 95; $stmt->execute(); echo "New record created successfully"; // Close statement and connection $stmt->close(); $conn->close(); ?>

keywords: PHP MySQL database insert results programming