0

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.

4
  • You must have to declare your variables as global, e.g global DB_HOST..... Commented Apr 25, 2017 at 7:05
  • You need to pass those constant variables through constructor at the time of creating a new object from class. Commented Apr 25, 2017 at 7:09
  • You have to create object for that class. $conn = new DbConnect(); Commented Apr 25, 2017 at 7:20
  • are you creating an instance of you class? If not you won't get any output. Btw. you don't output anything in your code, so no echo statement or something like that. Commented Apr 25, 2017 at 7:20

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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);
?>

2 Comments

Thanks for your help, This worked perfectly fine for me
Pleasure! Glad to help
0

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);

1 Comment

The output is empty, I don't receive any error message

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.