Hi Guys. I was browsing the Internet trying to solve my problem and finally I got to here. I have constants.php where I define my DB server, user, password and name, also I have connection.php where I parse these values.
constants.php
PHP Code:
// Database Constants
define("DB_SERVER", "localhost");
define("DB_USER", "root");
define("DB_PASS", "root");
define("DB_NAME", "widget_corp");
connection.php
PHP Code:
require("constants.php");
// 1. Create a database connection
$connection = mysql_connect("DB_SERVER", "DB_USER", "DB_PASS");
if (!$connection) {
die("Database connection failed: " . mysql_error());
}
// 2. Select a database to use
$db_select = mysql_select_db("DB_NAME", $connection);
if (!$db_select) {
die("Database connection failed: " . mysql_error());
}
content.php
PHP Code:
<?php require_once("includes/connection.php"); ?>
<?php require_once("includes/functions.php"); ?>
<?php include("includes/header.php"); ?>
<section id="sidebar">
<nav id="navigation">
<ul class="subjects">
<?php
$query = "SELECT *
FROM subjects
ORDER BY position ASC";
$subject_set = mysql_query($query, $connection);
confirm_query($subject_set);
while ($subject = mysql_fetch_array($subject_set)) {
echo "<li>{$subject["menu_name"]}</li>";
$query = "SELECT *
FROM pages
WHERE subject_id = {$subject["id"]}
ORDER BY position ASC";
$page_set = mysql_query($query, $connection);
confirm_query($page_set);
echo "<ul class=\"pages\">";
while ($page = mysql_fetch_array($page_set)) {
echo "<li>{$page["menu_name"]}</li>";
}
echo "</ul>";
}
?>
</ul>
</nav>
</section>
<section id="page">
<h2>Content Area</h2>
</section>
<?php require("includes/footer.php"); ?>
After I run my code I receive this error
Database connection failed: Unknown MySQL server host 'DB_SERVER' (2)
If I change all the variables in connection.php with their values it works fine. I use MAMP. Thank you.
Bookmarks