0

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>
  • possible duplicate of [Reference: What is variable scope, which variables are accessible from where and what are "undefined variable" errors?](http://stackoverflow.com/questions/16959576/reference-what-is-variable-scope-which-variables-are-accessible-from-where-and) – deceze Mar 11 '14 at 17:27
  • @deceze I would agree except my original variable that's being called "undefined" is first declared in a try-catch inside config.php - no scope issues, as far as I know. It should be global. – Christina Kline Mar 11 '14 at 17:30
  • ...and you are trying to use it where...?! – deceze Mar 11 '14 at 17:31
  • @deceze inside a function inside a file called after the config.php. According to the article you linked me to, it should work. Unless I'm missing something very simple. – Christina Kline Mar 11 '14 at 17:32
  • Then you need to read the article again closely. Inside a function is a new scope. – deceze Mar 11 '14 at 17:42
  • @deceze the section called "Scope and Included Files" is almost identical to my call (minus the fact that I'm using a database connection). See that little commented // works ? Mine should work as it is called the same way. If you actually see where my error is, then please let me know. – Christina Kline Mar 11 '14 at 17:45
  • Inside your function `menu` you are trying to use a variable `$dbh`. That variable is not passed into the function and is not declared inside the function, hence it's out of scope. That's all there is to it. – deceze Mar 11 '14 at 17:49
  • @deceze ah forgot the parameter. That's all you needed to say lol - I knew it was a simple miss. It is, otherwise, called correctly. – Christina Kline Mar 11 '14 at 17:51
  • @deceze simply passing the database handle $dbh threw a different error - Catalog error. Any ideas on that? – Christina Kline Mar 11 '14 at 17:59
  • What about googleing for the error? – Your Common Sense Mar 11 '14 at 18:12
  • I did. The results were very program specific (things from Github, mostly - and dealing with pieces of code well beyond my scope). – Christina Kline Mar 11 '14 at 18:28
  • Fixed that error - apparently the PDO database setup doesn't like spaces in the connection statement. Array isn't printing right but I think I can fix that - if so, I will return with a full answer to my own question. – Christina Kline Mar 11 '14 at 18:32

1 Answers1

0

It was a simple fix after all. Scope was correct, call was correct - simply forgot the parameter.

Once I added the $dbh call in the correct places, I started getting this error:

PDOStatement::execute(): SQLSTATE[3D000]: Invalid catalog name: 1046 No database selected in /CCU/STH Liquidations/scripts/GetData.php on line 10

After some trial and error, I figured out that the PDO database connection script doesn't like spaces.

Connected perfectly after that fix and have gotten my menu working properly, even able to pull in only those menu links set to true.

Days work done LOL!