0% found this document useful (0 votes)
7 views2 pages

PR 15 PHP

The document is a PHP script that connects to a MySQL database to retrieve and display a list of products from the 'shop_db' database. It checks for connection errors and fetches product details including ID, name, price, and creation date, displaying them in an HTML table. If no products are found, it shows a message indicating that no products are available.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views2 pages

PR 15 PHP

The document is a PHP script that connects to a MySQL database to retrieve and display a list of products from the 'shop_db' database. It checks for connection errors and fetches product details including ID, name, price, and creation date, displaying them in an HTML table. If no products are found, it shows a message indicating that no products are available.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

<?

php
// Database Connec on
$host = "localhost";
$user = "root";
$pass = ""; // Default MySQL password (empty in XAMPP)
$dbname = "shop_db";

// Create connec on using MySQLi


$conn = new mysqli($host, $user, $pass, $dbname);

// Check connec on
if ($conn->connect_error) {
die("Connec on failed: " . $conn->connect_error);
}

// Fetch data from database


$sql = "SELECT id, name, price, created_at FROM products";
$result = $conn->query($sql);
?>

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,
ini al-scale=1.0">
Product List
</head>
<body>
<h2 style="text-align:center;">Product List</h2>
<table>
<tr>
<th>ID</th>
<th>Name</th>
<th>Price ($)</th>
<th>Created At</th>
</tr>
<?php
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
echo "<tr>
<td>{$row['id']}</td>
<td>{$row['name']}</td>
<td>{$row['price']}</td>
<td>{$row['created_at']}</td>
</tr>";
}
} else {
echo "<tr><td colspan='4'>No products
found</td></tr>";
}
?>
</table>
</body>
</html>

<?php
$conn->close();
?>
<?php </form>
// Database Connec on
</body>
$host = "localhost";
$user = "root"; </html>
$pass = ""; // Default MySQL password (empty in XAMPP)
$dbname = "shop_db";

// Create connec on using MySQLi


$conn = new mysqli($host, $user, $pass, $dbname);

// Check connec on
if ($conn->connect_error) {
die("Connec on failed: " . $conn->connect_error);
}

// Fetch data from database


$sql = "SELECT id, name, price, created_at FROM
products";
$result = $conn->query($sql);
?>

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,
ini al-scale=1.0">
Product List
</head>
<body>
<h2 style="text-align:center;">Product List</h2>
<table>
<tr>
<th>ID</th>
<th>Name</th>
<th>Price ($)</th>
<th>Created At</th>
</tr>
<?php
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
echo "<tr>
<td>{$row['id']}</td>
<td>{$row['name']}</td>
<td>{$row['price']}</td>
<td>{$row['created_at']}</td>
</tr>";
}
} else {
echo "<tr><td colspan='4'>No products
found</td></tr>";
}
?>
</table>
</body>
</html>

<?php
$conn->close();
?>

You might also like