-1

I have a php file that calls a smarty template, it works and all of the variables are passing correctly. The php files that do not require a smarty template are connecting to the database with no problems. THE PROBLEM: anytime I enter a PDO statement the page loads as a blank html instead of the template.

<?php
session_name('login');
session_start();

if(!isset($_SESSION['username'])){
    header("Location: login.php");
}

include(connect.php);

function getCats()
{       

    $cat = $db->prepare("SELECT * FROM `categories`");
    $cat->execute();
    $categories = $cat->fetchAll();
    return $categories; 
}

require('./Smarty-3.1.13/libs/Smarty.class.php');
$smarty = new Smarty;
$smarty->assign("catinfo", getCats());
$smarty->assign("pageType", "1");
$smarty->display('index.tpl');
?>  

The smarty template section that deals with this looks like this.

{foreach from=$catinfo item='cat'}
    <div class="category">
     {foreach from=$cat item='info'}
            <div class="catAttribute">{$info}</div>
      {/foreach}

This is what the table contains:

CREATE TABLE `categories` (
`category`  VARCHAR(20),
`count` INT(10),
`message`   TEXT,
PRIMARY KEY(`category`)
);
vsmayberry
  • 67
  • 3
  • Does smarty template display correctly if there is no call to database? – eidsonator May 01 '13 at 00:32
  • The problem is that you have an error in your templates. Or... your templates aren't handling an error from PDO. Of course you can use PDO and Smarty together. – Brad May 01 '13 at 00:36
  • probably an error in your {foreach} or {section} when you iterate over the records – nathan hayfield May 01 '13 at 00:40
  • try adding `$smarty->_error_reporting = E_ALL;` before `$smarty->display..` and make sure you've `display_errors` `On` in PHP, `ini_set('display_errors', 1);` – Ejaz May 01 '13 at 00:41
  • The templates load fine if there is no pdo call and the pdo works if there is no smarty template involved. The $smarty->error_reporting did not catch anything. The template is throwing no errors, it just loads as a blank page. – vsmayberry May 01 '13 at 00:54
  • @vsmayberry I replied to your question not realizing that you were using smarty3. Your original foreach syntax wasn't wrong IMO – Ejaz May 01 '13 at 01:15
  • @Ejay I rewrote the template code as suggested and I am still getting the same result. I am pretty sure that the problem is in the php code, I put a die statement right after the prepare call to the database and it never executed. – vsmayberry May 01 '13 at 01:15
  • hah, you probably need a `global $db;` inside `function getCats(){..}`, that is if `$db` is defined at first place – Ejaz May 01 '13 at 01:17
  • I have the $db defined in a separate connect.php file so I can call it easily. Isn't that the same as a global $db? – vsmayberry May 01 '13 at 01:18
  • No, you need to make it accessible inside the `getCats()` by using `global $db;` before you attempt to use it. – Ejaz May 01 '13 at 01:24
  • It worked I just needed $db to be defined as global; – vsmayberry May 01 '13 at 01:28

1 Answers1

1
function getCats()
{       
    $cat = $db->prepare("SELECT * FROM `categories`");
    $cat->execute();
    $categories = $cat->fetchAll();
    return $categories; 
}

The $db variable, despite being declared in outer scope, is not accessible inside your function definition. So $db->prepare... throws an error and your script doesn't work from then on. To fix this problem, you should make this variable available inside your function by putting following statement before attempting to use $db variable.

global $db;

To avoid such overlook in future, you should keep error reporting turned on in your development scripts by putting following before your code

ini_set('display_errors', 1);
error_reporting(E_ALL);

This will make PHP report and display all notices, warnings, and errors which is very useful during development. You can also use a debugger extension like xdebug, which provides enhanced error reporting with backtraces (backtraces, I think)

Ejaz
  • 8,719
  • 3
  • 34
  • 49