What are some common ways to store image ratings in PHP applications?

Storing image ratings in PHP applications can be done by saving the ratings in a database table. Each image can have a corresponding row in the table with a column to store the rating value. This allows for easy retrieval and updating of ratings for each image.

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

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

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

// Insert a new rating for an image
$image_id = 1;
$rating = 4.5;

$sql = "INSERT INTO image_ratings (image_id, rating) VALUES ($image_id, $rating)";

if ($conn->query($sql) === TRUE) {
  echo "Rating stored successfully";
} else {
  echo "Error storing rating: " . $conn->error;
}

// Close the connection
$conn->close();