我知道有人问过类似的问题。但是,我找到的解决方案都无法解决我的问题。
我正在使用 Avada WordPress 主题。我为表格列表创建了一个自定义页面,该页面需要我连接到数据库才能查看元素。错误声明是
致命错误:无法在第 49 行的 /home/public_html/wp-content/themes/Avada/krtcsearcher.php 中重新声明类用户
- 尽管有错误消息,该函数仍然可以正常工作,因为它显示了来自数据库的消息。
- 该文件在使用 localhost-XAMPP 时运行良好(没有 wordpress php 代码)
- 我在课堂上尝试过 require_once 或 if(!class_exist) 但不起作用。
Krtcsearcher.php错误部分看起来像这样
<?php
// Template Name: krtcsearcher
?>
<?php get_header(); ?>
<div id="content" <?php Avada()->layout->add_class( 'content_class' ); ?> <?php Avada()->layout->add_style( 'content_style' ); ?>>
<?php while ( have_posts() ) : the_post(); ?>
<?php $page_id = get_the_ID(); ?>
<div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<?php echo avada_render_rich_snippets_for_pages(); ?>
<?php echo avada_featured_images_for_pages(); ?>
<?php
require_once '/home/public_html/wp-content/themes/Avada/config/connection.php';
if(!class_exists('DB_Connection')){
class DB_Connection {
private $connect;
function __construct() {
$this->connect = mysqli_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME)
or die("Could not connect to database");
}
public function getConnection()
{
return $this->connect;
}
}
}
class Users {
private $db;
private $connection;
function __construct() {
$this -> db = new DB_Connection();
$this -> connection = $this->db->getConnection();
//echo "Database Connection Successful!";
}
function secinfo($test) {
$sql = 'Select infoc FROM informations;
$result = mysqli_query($this->connection, $sql);
//$row = $result->fetch_assoc(); //get first row of the list
//while ($row = $result->fetch_assoc()) {
while ($row = $result->fetch_row()) {
echo $row[0];
}
mysqli_close($this->connection);
}
}
?>
新图片*