How can PHP beginners effectively utilize external files to streamline database queries?

PHP beginners can effectively utilize external files to streamline database queries by creating a separate file for their database connection details, such as hostname, username, password, and database name. This allows for easy modification of database credentials without having to update multiple files. By including this external file in their PHP scripts, beginners can streamline their database queries and ensure consistency across their codebase.

<?php
// db_config.php
$hostname = "localhost";
$username = "root";
$password = "password";
$database = "my_database";

// db_connection.php
require_once('db_config.php');
$connection = mysqli_connect($hostname, $username, $password, $database);

// query.php
require_once('db_connection.php');
$query = "SELECT * FROM users";
$result = mysqli_query($connection, $query);

// Fetch data from database
while($row = mysqli_fetch_assoc($result)) {
    echo $row['username'] . "<br>";
}

mysqli_close($connection);
?>