EDIT: New Question (Previous answered, stupid mistake)
I corrected the below code to call $dbh into the menu function (nav.php). However, now I am getting a new error that Google isn't helping me with (all answers are program specific):
PDOStatement::execute(): SQLSTATE[3D000]: Invalid catalog name: 1046 No database selected in /CCU/STH Liquidations/scripts/GetData.php on line 10
No database handle errors were thrown prior to trying to pull the menu in.
I am new to PDOs, so this might be a relatively simple question - but I couldn't find an answer close enough to my problem to help.
I am building my first true content management system. I am trying to bring in a menu based on whether it is to be shown (value is either 1 or 0, to be determined by admin).
However, in just trying to read them in, I'm getting undefined variable errors between files. The files are called in the correct order, and both are pulled in by index.php.
The variable that is undefined is the $dbh in the GetData.php file.
Do you see what I'm doing wrong? Because I can't see it.
index.php:
<?php
session_start();
$title = "STH Liquidations";
$description = "Product wholesale";
$keywords = "wholesale, product, pallets";
include "parts/_head.php";
include "parts/header.php";
include "parts/nav.php";
?>
<div id="content_index">
<?php include "parts/hot_deals.php"; ?>
<div id="image_holder">
<ul class="bxslider">
<li><img src="slider/pic1.jpg" title="Welcome to STH Liquidations!"/></li>
<li><a href="electronics.php"><img src="slider/pic2.jpg" title="Electronics"/></a></li>
<li><a href="furniture-and-appliances.php"><img src="slider/pic3.jpg" title="Furniture" /></a></li>
<li><a href="furniture-and-appliances.php"><img src="slider/pic4.jpg" title="Appliances" /></a></li>
<li><a href="tools.php"><img src="slider/pic5.jpg" title="Tools" /></a></li>
<li><a href="sporting-goods.php"><img src="slider/pic6.jpg" title="Sporting Goods"/></a></li>
</ul>
</div>
<p class="head1">Welcome to <strong>STH Liquidations, Inc.</strong></p>
<p>We at <strong>STH Liquidations, Inc.</strong> have been in the business of buying and selling overstock and liquidated NAME BRAND merchandise from <strong>major retailers</strong>, catalog companies, and big box stores for over 10 years now. Our goal has always been to consistently provide your business with <strong>dependable</strong> and <strong>trustworthy</strong> service along with the best possible pricing that will allow you to <strong>MAXIMIZE YOUR PROFITS</strong> and <strong>minimize your risk</strong> on every deal. Whether your business is wholesale, retail, auctions, online, flea markets, or if you are a “mom and pop store”, we are able to meet your specific needs.
</p>
<p>We carry truckloads of <a href="general-merchandise.php">general merchandise</a>, <a href="furniture-and-appliances.php">furniture</a>, <a href="housewares.php">housewares</a>, <a href="tools.php">tools</a>, <a href="toys.php">toys</a>, <a href="sporting-goods.php">sporting goods</a>, <a href="jewelry.php">jewelry lots</a>, <a href="apparel-and-shoes.php">apparel</a> and much more. We ship direct from the reclaim centers, which eliminates “cherry picking” and keeps <strong>YOUR COST LOW</strong>. Our simple philosophy; <strong>money saved is money made</strong>. Your success is our success.</p>
<p>Please feel free to browse our website at your leisure and CALL us with ANY questions you may have to <strong>place your order</strong>.</p>
<p>Join our FREE mailing list <a href="contact.php">HERE</a> to receive <strong>up to date listings</strong> and our <strong>HOT deals</strong>.</p>
</div>
<div id="hotdeals">
<p class="head2">Hot Deals!</p>
<div class="deals">
<p class="deal_title">K-Hardgoods</p>
<p class="deal_desc">Truckloads, general merchandise, tools, toys housewares and more</p>
<p class="deal_price">as low as $139 per pallet</p>
<p class="deal_pdf">Call for Information!</p>
</div>
<div class="deals">
<p class="deal_title">SRS Tool Truckload</p>
<p class="deal_desc">CR*STSM*N TOOLS AND MUCH MORE <br />Saws, compressors, blowers, edgers. saber saws, table saws and much more</p>
<p class="deal_price">27 PALLETS--WHLS $66,649.32 <br />SELL PRICE $12,900</p>
<p class="deal_pdf"><a href="PDFs/srs_tools_012914.pdf" target="_blank">Download PDF</a></p>
</div>
<div class="deals">
<p class="deal_title">W*M Power wheels</p>
<p class="deal_desc">Ride on toy truckloads
<br />150-180 units per truckload
<br />Customer returns</p>
<p class="deal_price">Price only $5,900</p>
</div>
</div>
<div class="clear"></div>
<?php
include "parts/footer.php";
?>
_head.php:
<!DOCTYPE html>
<html>
<?php
require "config.php";
require_once "scripts/GetData.php";
?>
<head>
<!-- NAME THE PAGE -->
<title><?php $title ?></title>
<!-- GET THE FAIRY DUST AND DUST BUNNIES -->
<link rel="stylesheet" type="text/css" href="scripts/basic.css" />
<script type="text/javascript" src="contact-files/contact-form.js"></script>
<script type="text/javascript" src="scripts/jquery.min.js"></script>
<!-- bxSlider -->
<script src="scripts/jquery.bxslider.min.js"></script>
<link href="scripts/jquery.bxslider.css" rel="stylesheet" />
<script src="scripts/muscles.js"></script>
<!-- TELL GOOGLE WHAT IT WANTS TO HEAR -->
<meta name="description" content="<?php $description ?>">
<meta name="keywords" content="<?php $keywords ?>">
<!-- FIX ENCODING ERROR -->
<meta charset="UTF-8">
</head>
<!-- =============== -->
<!-- HEADER -->
<!-- =============== -->
config.php:
<?php
$host = "localhost";
$user = "root";
$pass = "root";
$database_name = "sthliquidations";
// Create connection
try
{
// PDO Connection
$dbh = new PDO("mysql:host = $host; dbname = $database_name", $user, $pass);
}
catch (PDOException $e)
{
echo $e -> getMessage();
}
// Show me any exceptions
// TURN THIS OFF WHEN LIVE
$dbh -> setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
?>
GetData.php (and where the error is being thrown):
<?php
function menu ($dbh)
{
// Make a call to the database
$menu_handle = $dbh->prepare("
SELECT * FROM cats
WHERE menu = ?;
");
$menu_handle->execute(array(1));
// Testing
$row = $menu_handle->fetch();
echo "<pre>";
echo $row;
echo "</pre>";
}
?>
nav.php (what's making the call in the first place):
<!-- NAVBAR -->
<div id = "sidenav">
<div id = "leftnav">
<ul class = "menulink">
<li><a href="index.php" alt="Home">Home</a></li>
<li><a href="vision.php" alt="Our Vision">About Us</a></li>
<?php menu($dbh); ?>
<li><a href="logistics.php" alt="Logistics">Freight Services</a></li>
<li><a href="contact.php" alt="Contact Us">Contact Us</a></li>
<li><a href="glossary.php" alt="Glossary of Terms">Glossary</a></li>
</ul>
</div>
</div>