3

I understand that similar questions have been asked. However, none of the solutions I found can solve my problem.

I am using the Avada WordPress theme. I have created a customized page for a table list that requires me to connect to the database to view elements. The error statement is

Fatal error: Cannot redeclare class Users in /home/public_html/wp-content/themes/Avada/krtcsearcher.php on line 49

  • Despite error message, the function still works well as it displays the message from the database.
  • The file is working well while using localhost-XAMPP (Without wordpress php code)
  • I have tried require_once or if(!class_exist) on the class but does not work.

Krtcsearcher.php The error part looks something like that

<?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);

          }

       } 

    ?>

NEW Pictures*

Everything is working fine but there is an error message shown below. Web Domain

From Localhost-XAMPP XAMMP-Localhost

Martin
  • 22,212
  • 11
  • 70
  • 132
Ben Chin
  • 31
  • 6
  • `Fatel Error` and `Despite error message, the function still works well as it displays the message from the database.` ? I am astonished – Alive to die - Anant Jun 30 '16 at 08:09
  • Your problem is with class Users and not with db_connection, it is defined somewhere else, if it is the same definition, just remove the one in this file, or rename it – Gar Jun 30 '16 at 08:14
  • Yeah just rename your class and all will be ok. – Simon Pollard Jun 30 '16 at 10:52
  • Renaming would just turn out with the same error. My sense is the error has something to do with overlapping of WP code. – Ben Chin Jul 02 '16 at 10:46
  • you should probably namespace your class so this sort of thing doesn't happen. – Martin Jul 02 '16 at 10:55

0 Answers0