How can I retrieve and display data from a database using PHP without using Smarty?
To retrieve and display data from a database using PHP without using Smarty, you can use PHP's built-in MySQLi or PDO extension to establish a connection to the database, execute a query to fetch the data, and then loop through the results to display them on the webpage.
<?php
// Establish a connection to the database
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database";
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Execute a query to fetch data
$sql = "SELECT * FROM table";
$result = $conn->query($sql);
// Loop through the results and display them
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "ID: " . $row["id"] . " - Name: " . $row["name"] . "<br>";
}
} else {
echo "0 results";
}
// Close the connection
$conn->close();
?>