0

I've got this code:

<?php
class DBconnection extends Mysqli {
    public $mysql;
    public function __construct($host, $user, $pass, $db) {
        $mysql = parent::__construct($host, $user, $pass, $db);     
    }
}

$mysql = new DBconnection('localhost', '**', '**', '**');
?>

But i cant make connection in a other class.

<?php
require_once('./mysqli.php');

class Activator {
    private $key;
    public $status;

    public function __construct($key) {
        $this->key = $key;
        $this->status = array();

        $this->activate();
    }

    private function activate() {
        $query = $mysql->query("SELECT email, COUNT(email) AS founds FROM activation WHERE activationKey = '".$mysql->real_escape_string($this->key)."' LIMIT 1");

        if($query) {
            $query = $query->fetch_assoc();

            if($query['founds'] == 0) {
                $this->status['error'] = 'Deze code is verlopen of ongeldig.';
            } else {
                if($mysql->query("UPDATE users SET activation = '1' WHERE email = '".$mysql->real_escape_string($query['email'])."'")) {
                    $this->status['res'] = 'Uw account is geactiveerd!';
                } else {
                    $this->status['error'] = 'Oeps, een fout! Neem contact op per email als dit over 5 minuten nog steeds zo is. Exuses voor het ongemak.';
                }
            }
        } else {
            $this->status['error'] = 'Oeps, een fout! Neem contact op per email als dit over 5 minuten nog steeds zo is. Exuses voor het ongemak.';
        }
    }
}
?>

How can i make it work?

This is the error:

Fatal error: Cannot redeclare class DBconnection in /home/raoul/domains/internetfaq.nl/public_html/dev/mysqli.php on line 6

Abhineet Verma
  • 1,008
  • 8
  • 18
Jesper V
  • 25
  • 1
  • 1
  • 7

1 Answers1

2

You need to make $mysqli available inside of the class. It isn't available due to scope. This can be overcome by passing it to the activate() method as a parameter:

private function activate($mysqli) {

and called:

$activator->activate($mysqli);

You should not be declaring that class more than once. Put that class definition in its own file and then include that file on each page that requires it.

DBconnection.php

<?php
class DBconnection extends Mysqli {
public $mysql;
public function __construct($host, $user, $pass, $db) {
    $mysql = parent::__construct($host, $user, $pass, $db);     
}
}

file1.php

<?php
require_once('DBconnection.php');
$mysql = new DBconnection('localhost', '**', '**', '**');
 ?>

file2.php

<?php
require_once('DBconnection.php');
$mysql = new DBconnection('localhost', '**', '**', '**');
 ?>

There are better ways to do this but this starts you down the road to better organized code.

John Conde
  • 217,595
  • 99
  • 455
  • 496