I am new to working with PHP and Mysql Database.
I am using MAMP's phpmyadmin to create database.
By below code works fine and establishes connection with the server:
<?php
include "config.php";
$mysqli = new mysqli(DB_HOST, DB_USERNAME, DB_PASSWORD, DB_DATABASE);
if (mysqli_connect_error()) {
die('Connect Error ('.mysqli_connect_errno().') '.mysqli_connect_error());
}
echo 'Connected successfully.';
?>
But, i have a problem with connecting when i use constructor and a class :
Config.php:
<?php
define("DB_HOST", "localhost");
define("DB_USERNAME", "root");
define("DB_PASSWORD", "root");
define("DB_DATABASE", "scapik_users");
define("DB_PORT","8889");
?>
DbConnect.php
<?php
include "config.php";
class DbConnect{
private $connect;
public function __construct(){
$this->connect = new mysqli(DB_HOST, DB_USERNAME, DB_PASSWORD, DB_DATABASE);
if (mysqli_connect_error()) {
die('Connect Error ('.mysqli_connect_errno().') '.mysqli_connect_error());
}
}
public function getDb(){
return $this->connect;
}
}
?>
Could someone help me correct the code, I guess some where i am going wrong.
Thanks.
$conn = new DbConnect();echostatement or something like that.