Gitlab@Informatics

Skip to content
Snippets Groups Projects
Commit 7856506a authored by 65160264's avatar 65160264
Browse files

Edit connect index.php

parent 1c43e53c
Branches
No related tags found
No related merge requests found
<?php
echo "Ruk buaruang 65160264";
// Database configuration
$servername = "db";
$username = "Lab2";
$password = "Ruk241246<3";
$dbname = "Lab2";
try {
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
throw new Exception("Connection failed: " . $conn->connect_error);
}
// Query to fetch students
$sql = "SELECT id, fname, lname FROM student";
$result = $conn->query($sql);
// Display results
if ($result->num_rows > 0) {
// output data of each row
echo "<h2>Student List</h2>";
echo "<ul>";
while ($row = $result->fetch_assoc()) {
echo "id: " . $row["id"]. " - Name: " . $row["fname"]. " " . $row["lname"]. "<br>";
echo "<li><strong>ID:</strong> {$row['id']} - <strong>Name:</strong> {$row['fname']} {$row['lname']}</li>";
}
echo "</ul>";
} else {
echo "0 results";
echo "<p>No students found.</p>";
}
} catch (Exception $e) {
echo "<p style='color:red;'>Error: " . $e->getMessage() . "</p>";
} finally {
// Close connection
$conn->close();
}
?>
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment