Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/291.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用pdo时,php不会加载smarty_Php_Pdo_Smarty3 - Fatal编程技术网

使用pdo时,php不会加载smarty

使用pdo时,php不会加载smarty,php,pdo,smarty3,Php,Pdo,Smarty3,我有一个调用smarty模板的php文件,它工作正常,所有变量都正确传递。不需要smarty模板的php文件可以毫无问题地连接到数据库。问题是:每当我输入PDO语句时,页面都会作为空白html而不是模板加载 <?php session_name('login'); session_start(); if(!isset($_SESSION['username'])){ header("Location: login.php"); } include(connect.php);

我有一个调用smarty模板的php文件,它工作正常,所有变量都正确传递。不需要smarty模板的php文件可以毫无问题地连接到数据库。问题是:每当我输入PDO语句时,页面都会作为空白html而不是模板加载

<?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');
?>  
$db
变量尽管在外部范围内声明,但在函数定义内是不可访问的。因此,
$db->prepare…
抛出一个错误,您的脚本从此无法工作。要解决此问题,您应该在尝试使用
$db
变量之前,通过在函数中放入以下语句,使此变量可用

global $db;
为了避免将来出现这样的疏忽,您应该在开发脚本中打开错误报告,方法是将以下内容放在代码之前

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

这将使PHP报告并显示在开发过程中非常有用的所有通知、警告和错误。您还可以使用诸如
xdebug
之类的调试器扩展,它通过回溯(我想是回溯)提供了增强的错误报告功能。

如果没有对数据库的调用,smarty template是否正确显示?问题是您的模板中存在错误。或您的模板没有处理来自PDO的错误。当然,您可以同时使用PDO和Smarty在
$smarty->display..
之前,并确保您在PHP中的
ini\u集('display\u errors',1)上有
显示错误
如果没有pdo调用,则模板加载良好;如果没有涉及smarty模板,则pdo工作。$smarty->error\u报告没有捕获任何内容。模板没有抛出错误,只是作为空白页加载。
function getCats()
{       
    $cat = $db->prepare("SELECT * FROM `categories`");
    $cat->execute();
    $categories = $cat->fetchAll();
    return $categories; 
}
global $db;
ini_set('display_errors', 1);
error_reporting(E_ALL);