How can annotations be stored in a database and retrieved using PHP?

Annotations can be stored in a database by creating a table with columns for the annotation text, the associated document or object ID, and any other relevant information. To retrieve annotations using PHP, you can query the database for annotations related to a specific document or object ID.

// Connect to the database
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "annotations";

$conn = new mysqli($servername, $username, $password, $dbname);

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

// Retrieve annotations for a specific document or object ID
$document_id = 1;
$sql = "SELECT * FROM annotations WHERE document_id = $document_id";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // Output data of each row
    while($row = $result->fetch_assoc()) {
        echo "Annotation: " . $row["annotation_text"]. "<br>";
    }
} else {
    echo "0 results";
}

$conn->close();