繁体   English   中英

PHP未定义变量或无法在包含文件中重新声明类消息

[英]php undefined variable or cannot redeclare class message inside included file

我不再使用PHPMySQL而是尝试开发一个练习站点。 但是我面临一些麻烦。 这个网站对我来说最有用,因此经过大量尝试,我决定在这里询问。

我的问题是我已经为页眉,页脚,侧栏制作了部分分隔的文件,并将那些文件包括在索引,产品或其他文件中。 当我尝试从index.php获取数据库结果时,它工作正常,但我还必须在sidebar.php中创建类别ID,这也应该从数据库中获取。

当我试图在index.php中获取数据时工作正常,但在sidebar.php中尝试获取数据时,它警告我未定义的变量,当我也在sidebar.php中包含数据库类时,它警告我无法重新声明类databse。 有我的代码和文件夹结构。

注意:关于我的知识,在非常乞讨的情况下包括了initialize.php index.php,之后包括了header.php,因此它应该使用initialize.php,但是我已经在sidebar.php中尝试了include初始化,但它会生成错误“无法重新声明类数据库”。 当我从sidebar.php删除此初始化时,它会生成错误“未定义的变量连接” ...当我工作为单个文件时,我的意思是我删除了sidebar.php并直接在index.php内编写孔代码,效果很好。

我的文件夹结构就是这样。

| Practice_site
|---| includes
|---|---| class
|---|---|---| database.class.php
|---|---|---| validation.class.php
|---|---|---| upload.class.php
|---|---| functions.php
|---|---| initialize.php
|---| public
|---|---| site_template
|---|---|---| header.php
|---|---|---| footer.php
|---|---|---| sidebar.php
|---|---| index.php
|---|---| about.php
|---|---| products.php
|---|---| product_detail.php
|---|---| cart.php  

in Databse.class.php file

class Database
{
private $connection;
private $error;
private $stmt;
// Set options
private $options = array(
    PDO::ATTR_PERSISTENT => true, 
    PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
);

public function __construct($host,$dbname,$uname,$dbpassword="")
{
    try{
        $dns = "mysql:host=".$host.";dbname=".$dbname;
        $this->connection = new pdo($dns,$uname,$dbpassword,$this->options);    
    }catch(PDOException $e){
        $this->error = $e->getMessage();
    }

    (isset($this->error))? die("<h3>".$this->error."</h3>"):null;
}

public function query($sql)
{
    return $this->connection->query($sql);
}

public function fetch()
{
    return $this->connection->fetch();
}

public function rowCount()
{
    return $this->connection->rowCount();
}

public function fetchAll()
{
    return $this->stmt->fetchAll(PDO::FETCH_ASSOC);
}

}//end class
$connection = new Database('localhost','dbname','user','password');

in function.php file

in header.php file

<!DOCTYPE html>
<html>
<head>
    <title>title or the page</title>
</head>
<body>
<?php include "sidebar.php"; ?>

in initialize.php file

<?php 
defined('DS')? null: define('DS', DIRECTORY_SEPARATOR);
defined('SITE_ROOT')? null: define('SITE_ROOT', 'C:'.DS.'wamp'.DS.'www'.DS.'OnlineStore');
defined('LIB') ? null : define('LIB', SITE_ROOT.DS.'includes');

include LIB.DS."class".DS."Database.class.php";
include LIB.DS."functions.php";
?>

in sidebar.php file

try{
    require "../includes/initialize.php";  // when remove this line php generate undefined variable connection else generate error cannot redeclare class databas line 9
    $sql = "SELECT * FROM category ORDER BY position";
    $categories = $connection->query($sql);
}catch(EXCEPTION $e){
    $error = $e->getMessage();
}

<?php while ( $category = $categories->fetch() ): ?>
      <li><a href="#"><?php echo $category['category']; ?></a></li>
<?php endwhile; ?>

in index.php file

<?php 
try{
    // require "../includes/initialize.php";
    $sql = "SELECT * FROM products ORDER BY product_id DESC";
    $products = $connection->query($sql);

    $sql = "SELECT * FROM category ORDER BY position";
    $categories = $connection->query($sql);

}catch(EXCEPTION $e){
    $error = $e->getMessage();
}
include "header.php";

<?php while( $row = $products->fetch() ): ++$counter ?>
<div class="product_box <?php echo ( ($counter%3)===0 ) ? 'no_margin_right' : '' ?>">
    <h3><?php echo htmlentities($row['product_name']) ?></h3>
    <a href="productdetail.php"><img src="images/products_img/<?php echo $row['product_image']; ?>" alt="Shoes 3" /></a>
    <p><?php echo htmlentities(substr($row['description'], 0,100)) ?></p>
  <p class="product_price">$ 60</p>
    <a href="shoppingcart.php" class="addtocart"></a>
    <a href="productdetail.php" class="detail"></a>
</div>   
<?php echo ( ($counter%3)===0 ) ? '<div class="cleaner"></div>' : '' ?>
<?php endwhile; ?>

尝试require_onceinclude_once

另外,请注意此问题及其注释,因为在这里也可能是一个问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM