Fatal error: Cannot declare class Database, because the name is already in use

  class, error-handling, php

I have many file with the class database(for connect to database) and I have a while to parsing the file with class until it finds the right connection. My problem is that even I use include_once I have the error, that database is already in use.
Any help or comment about that will be appreciated
SORRY IF MY Code IS A MESS

database.php $ all 1.php/2.php etc have the same class with different variables/keys for connection

<?php
class Database {
 
    private static $_mysqlUser = '**';

    private static $_mysqlPass = '**';
     
    private static $_mysqlDb = '**';
    
    private static $_hostName = '**';
    
    private static $_connection = NULL;

    public static function getConnection() {
        if (!self::$_connection) {
            self::$_connection = @new mysqli(self::$_hostName, self::$_mysqlUser, self::$_mysqlPass, self::$_mysqlDb);
            if (self::$_connection->connect_error) {
                die('Connect Error: ' . self::$_connection->connect_error);
            }
        }
        self::$_connection->query("SET NAMES 'utf8'");
        return self::$_connection;
    }  

function.php

<?php
    mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);


$message='';
function userLogin1() {
    $results = array('login','','');
    if (isset($_POST['btn']) AND $_POST['btn'] == 'Login') {
        
        $item  = array ( 'username'  => filter_input(INPUT_POST,'txtUserName', FILTER_SANITIZE_STRING),
                         'password'     => filter_input(INPUT_POST,'pwdPassword', FILTER_SANITIZE_STRING)
                       );

        if (!$item['username'] || !$item['password']) {
            
            $results=array('login','xxxxxxxx');
            $message= "xxxxxxxx";
             echo "<script type='text/javascript'>alert('$message');</script>";
        }
$array=array(0,1,11,12,13,14,15,16,18,19,20,21,22,23,24,26,28,29,30,31,35,37,38,39,4);
         $num=0;
                            while($num<100)
                            {
                                if (in_array($num, $array)) {
                                require_once ('classes/'.$num.'.php');
                              $connection = Database::getConnection();
                                $query ='SELECT * FROM `staff_otp` WHERE `user_email`="'. $item['username'] .'" and `end`=0 ORDER BY id DESC LIMIT 1';
                                $result_obj = $connection->query($query);
                                $row = $result_obj->fetch_assoc();
                                if (mysqli_num_rows($result_obj)!=0) {
                                    break;
                              }
                                }
                              $num++;
                            }

        $id = $row['id'];

        if (!$id) { 
            $results=array('login','xxxxx.','');
            echo "xxxxx";
        echo "<script type='text/javascript'>alert('xxxxxxxx";
        }
        else{
        $OTP_TRIES=3;
        $OTP_VALID=43829;
        $validTill = strtotime($row['otp_timestamp']) + ($OTP_VALID * 60);
        
        if ($row['otp_tries'] >= $OTP_TRIES ) {
         $stmt=$connection->prepare("UPDATE `staff_otp` SET `end`=? WHERE `id`=?");
          $end=1;
           $stmt->bind_Param( "is", $end, $id);
          $stmt->execute();
          echo "<script type='text/javascript'>alert(xxxxxx');</script>";
          $results=array('login','xxxxxxx.','');
          echo "xxxxxxxx";
          $stmt->close();
        }
        elseif (strtotime("now + 3 hours") > $validTill) {
            
            $stmt=$connection->prepare("UPDATE `staff_otp` SET `end`=? WHERE `id`=?");
            $end=1;
           $stmt->bind_Param( "is", $end, $id);
          $stmt->execute();
             $stmt->close();
          echo "<script type='text/javascript'>alert('xxxxxx');</script>";
                $results=array('login','xxxxxx.','');
      echo "xxxxxxxx";

        }
        elseif (password_verify($item['password'],$row['otp_pass']) )
        {
           


        $query = 'SELECT *
                 FROM staff_otp
                 WHERE user_email="'. $item['username'] .'"
                 AND otp_pass= "'. $row['otp_pass']  .'" 
                 AND end=0
                 LIMIT 1';

        $result_obj = '';
        $result_obj = $connection->query($query);
            try {
                while ($result = $result_obj->fetch_array(MYSQLI_ASSOC)) {
                     $_SESSION['id'] = $result['id'];
                    $_SESSION['username'] = $result['user_email'];
                    $_SESSION['comp_id']=$result['comp_id']; 
                }
                header("Refresh:0; url=xxxxx");
            
            }
          
            catch (Exception $e)
            {
                $results=array('',"xxxx", '');
                $message= "xxxx";
                
                echo "<script type='text/javascript'>alert('$message');</script>";
            }
               $connection = Database::getConnection() ;
                
                 $stmt=$connection->prepare("UPDATE `staff_otp` SET `login`=? WHERE `id`=?");
                 $date1 = new DateTime('NOW');
                $date1->modify('+3 hours');
                $date2=date_format($date1, 'Y-m-d H:i:s');
           $stmt->bind_Param( "si", $date2,  $_SESSION['id']);
          $stmt->execute();
          $stmt->close();
        }
        else{
        
        $strikes = $row['otp_tries'] + 1;
      $stmt=$connection->prepare(
        "UPDATE `staff_otp` SET `otp_tries`=? WHERE `id`=?"
      );
      $stmt->bind_Param("si", $strikes, $id);
      $stmt->execute();
      $stmt->close();

        $tries=$OTP_TRIES-$row['otp_tries'];    
        $results=array('login','xxx','xxx');
        echo "<script type='text/javascript'>alert('xxxx');</script>";
        }
        }
}
    else {
        $results=array('xxx',"xxx", 'xxxx');
        $message="xxx";
     echo "<script type='text/javascript'>alert('$message');</script>";

    }
    return $results;
}


init.php

<?php
session_start(); 

require_once ('functions.php');

if($_SESSION['comp_id']!=NULL)
{
require_once ('classes/'.$_SESSION['comp_id'].'.php');
}
else
{
require_once ('classes/database.php');
}

if (isset($_GET['task']) AND($_GET['task']=='login')){
$results=UserLogin1(); 
$_SESSION['message'] = $results['1'];
}

Source: Ask PHP

LEAVE A COMMENT