0% found this document useful (0 votes)
14 views1 page

Main PHP

This PHP script connects to a MySQL database to retrieve and display an image based on its ID from the URL. It checks for a valid image ID, prepares and executes a SQL query to fetch the image type and data, and then outputs the image with the appropriate Content-Type header. If the image ID is not provided or the image is not found, it returns an error message.

Uploaded by

job931477
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views1 page

Main PHP

This PHP script connects to a MySQL database to retrieve and display an image based on its ID from the URL. It checks for a valid image ID, prepares and executes a SQL query to fetch the image type and data, and then outputs the image with the appropriate Content-Type header. If the image ID is not provided or the image is not found, it returns an error message.

Uploaded by

job931477
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

<?

php
// Database connection details
$servername = "your_servername";
$username = "your_username";
$password = "your_password";
$dbname = "your_dbname";

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

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

// Get the image ID from the URL (e.g., image.php?id=1)


if (isset($_GET['id'])) {
$id = $_GET['id'];

// Prepare and execute the SQL query


$stmt = $conn->prepare("SELECT image_type, image_data FROM images WHERE id
= ?");
$stmt->bind_param("i", $id);
$stmt->execute();
$stmt->bind_result($image_type, $image_data);

if ($stmt->fetch()) {
// Set the Content-Type header
header("Content-Type: " . $image_type);

// Output the image data


echo $image_data;
} else {
echo "Image not found.";
}

$stmt->close();
} else {
echo "Image ID not provided.";
}

$conn->close();
?>

You might also like