Reputation: 5393
Hi I just finished reading a beginner book on PHP and I wana start to create my own login.I did not get very far and I got an error that I can;t seem to fix.
I have 3 files
index.php
session_start();
require 'DB_ACCES/connect.php';
if(isset($_POST['submit'])){
$conn = new Mysqli();
}
constants.php
define('DB_HOST', 'localhost');
define('DB_USER' , 'root');
define('DB_PWD' , '');
connect.php
require '../includes/constants.php';
class Mysqli{
private $conn;
function __construct() {
$this->conn = new mysqli(DB_HOST , DB_USER , DB_PWD) or die(mysqli_error());
}
}
When I run index.php I get this error:
Fatal error: Cannot redeclare class Mysqli in D:\Program Files\xampp\htdocs\MyWork\Blog\DB_ACCES\connect.php on line 4
What am I doing wrong here and how can I repair it?
Upvotes: 0
Views: 5119
Reputation: 718
Mysqli already exists in php, since php is case insensitive the word mysqli it self is reserved and can't be used in any spelling what so ever.
you will be able to create your own class which extends mysqli, so it'll basically inherit all attributes and methods of the existing mysqli class
class MyMySQLi extends MySQLi
{
private $conn;
function __construct() {
$this->conn = parent::__construct(DB_HOST , DB_USER , DB_PWD) or die(mysqli_error());
}
}
Upvotes: 2
Reputation:
Your PHP environment includes mysqli which is a module that makes available a class named mysqli. Just name your class something else.
Upvotes: 0
Reputation: 41448
The bug is exactly what the error states.
Mysqli is an existing class that's part of a very common php shared library mysqli
. You can't redeclare it.
Change your classname to MyMysqli
or something.
class MyMysqli{
private $conn;
function __construct() {
$this->conn = new mysqli(DB_HOST , DB_USER , DB_PWD) or die(mysqli_error());
}
}
Upvotes: 3