harsh
harsh

Reputation: 3

Php & Database connection Error

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.

Upvotes: 0

Views: 638

Answers (3)

Nidhi
Nidhi

Reputation: 1539

create object of class

$obj = new DbConnect();

then run your code

<?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());
            } else {
            echo "connection successfull";
            }

        }

        public function getDb(){
            return $this->connect;
        }
    }
    $obj = new DbConnect();

?>

may be it will help you.

Upvotes: 1

chaensel
chaensel

Reputation: 183

Try adding your port information in the constructor. Since you defined it, it may be needed to connect to the database, as it may only be listening on that port.

EDITED my answer with full code example - this works on my side

config.php

<?php
define("DB_HOST", "localhost");
define("DB_USERNAME", "root");
define("DB_PASSWORD", "root");
define("DB_DATABASE", "test");
define("DB_PORT","3306");

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, DB_PORT);
    if (mysqli_connect_error()) {
        die('Connect Error ('.mysqli_connect_errno().') '.mysqli_connect_error());
    }
}

public function getDb(){
    return $this->connect;
}
}
$db = new DbConnect();
print_r($db->getDb());
?>

This works fine on my side... So I guess your code is fine.

I guess you have already checked your user credentials and the port and stuff... ?

Did you try setting your error reporting?

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

Upvotes: 0

Rotimi
Rotimi

Reputation: 4825

Take adavantage of Object oriented programming. Pass your databse paramters when initializing the DbConnect class. This would help you use the initialized variables throughout this class as well.

<?php
include "config.php";

    class DbConnect{

        private $connect;
        private $host;
        private $username;
        private $password;
        private $database;
        public function __construct($host, $username, $password, $database){
            $this->host = $host;
            $this->username= $username;
            $this->password = $password;
            $this->database = $database;
            $this->connect = new mysqli($this->host, $this->username, $this->password, $this->database);

            if (mysqli_connect_error()) {
            die('Connect Error ('.mysqli_connect_errno().') '.mysqli_connect_error());
            }

        }

        public function getDb(){
            return $this->connect;
        }
    }
?>
<?php
    define("DB_HOST", "localhost");
    define("DB_USERNAME", "root");
    define("DB_PASSWORD", "root");
    define("DB_DATABASE", "scapik_users");
    define("DB_PORT","8889");
?>
<?php
$host = DB_HOST;
$username = DB_USERNAME;
$password = DB_PASSWORD;
$database = DB_DATABASE;

#call this class as:
$class = new DbConnect($host, $username, $password, $database);
?>

Upvotes: 0

Related Questions