What are common issues with specifying the database location in PHP code, and how can they be resolved?
Common issues with specifying the database location in PHP code include hardcoding the database connection details directly in the code, making it difficult to update or maintain. To resolve this, it's recommended to store the database connection details in a separate configuration file outside of the web root directory for security purposes.
<?php
// config.php
define('DB_HOST', 'localhost');
define('DB_USER', 'username');
define('DB_PASS', 'password');
define('DB_NAME', 'database_name');
```
```php
<?php
// index.php
require_once('config.php');
$connection = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
if ($connection->connect_error) {
die("Connection failed: " . $connection->connect_error);
}