0
DbConnect.php

<?php

/**
 * Handling database connection

 */
 include_once ('Config.php');

class DbConnect {

    private $conn;

    function __construct() { 

    }

    /**
     * Establishing database connection
     * @return database connection handler
     */
    function connect() {

        // Connecting to mysql database
        $this->conn = new mysqli_connect(DB_HOST, DB_USERNAME, DB_PASSWORD, DB_NAME);

        // Check for database connection error
        if (mysqli_connect_errno()) {
            echo "Failed to connect to MySQL: " . mysqli_connect_error();
            exit;
        }


        // returing connection resource
        return $this->conn;
    }

}
 class DbHandler {

private $conn;

function __construct() {

    // opening db connection
    $db = new DbConnect();
    $this->conn = $db->connect();
}}

?>

config.php

<?php
/**
 * Database configuration
 */
define('DB_USERNAME', 'medma');
define('DB_PASSWORD', 'medma');
define('DB_HOST', 'localhost');
define('DB_NAME', 'medma_sameer');


/**
 * MSG91 configuration
 */
define('MSG91_AUTH_KEY', "130558AmTMgKzL582187bb");
// sender id should 6 character long
define('MSG91_SENDER_ID', 'MEDMA1');

define('USER_CREATED_SUCCESSFULLY', 0);
define('USER_CREATE_FAILED', 1);
define('USER_ALREADY_EXISTED', 2);
?>

Please tell me what could be the problem though i have posted before Not able to hit api to verify otp ( Used Volley) but when i started testing each php file on my localhost folder where i have mounted i got stuck with the very initial step which is database connection the code i wrote above doesn't show anything except blank

Community
  • 1
  • 1
SameerKhan1406
  • 289
  • 5
  • 18
  • Could you please try adding 127.0.0.1 instead of localhost into the config? Also what is the error message? Add debugging to your php code http://stackoverflow.com/questions/845021/how-to-get-useful-error-messages-in-php so you can see the error message in the browser – hmrc87 Nov 09 '16 at 12:55
  • 1
    Add `error_reporting(E_ALL); ini_set('display_errors', 1);` at top and then look what the output is – Peon Nov 09 '16 at 12:58

2 Answers2

2

There are 2 problems here that could cause the page to be blank:

The first is that you don't actually output anything in your script, at least not in the code you have posted.

The bigger problem is that this is probably wrong as you don't seem to be inside a method / class:

 $db = new DbConnect();
 $this->conn = $db->connect();

$this is defined in the scope of a method so using it like you are doing would lead to a fatal error:

Fatal error: Using $this when not in object context in ...

As a side-note: If you want to show the contents of a variable, you should use var_dump() instead of echo because the last only gives any meaningfull output for numbers and strings; not for database connections.

jeroen
  • 91,079
  • 21
  • 114
  • 132
  • And as @hrmc87 writes, some connections needs the port number (mamp for example), so the access url must be localhost:8888/ ( leave localhost for the db server) – Benoti Nov 09 '16 at 13:25
  • @Benoti Could be, the error handling the OP already has, would show that when the code is used. – jeroen Nov 09 '16 at 13:28
  • @Benoti we don't need to mention the default port and jeroen i'll do this and come back if anything is wrong. – SameerKhan1406 Nov 10 '16 at 04:36
1

First of all you bad invoke a connection here

 $db = new DbConnect();
 $this->conn = $db->connect();

You can not access property conn in this scope (outside of a class) this way You can do this using $db->conn. However in this situation you can't access it aswell as it is a private

If you would like to do this using your class you can try

$db = new DbConnect();
$conn = $db->connect();

and under $conn u have a db connection.

@edit: Maybe try use this

<?php
include_once ('Config.php');

/**
 * Handling database connection
 */
class DbConnect {

    private static $_instance; //The single instance
    private $_conn;

    public static function getInstance() {
       if(!self::$_instance) { // If no instance then make one
          self::$_instance = new self();
       }
       return self::$_instance;
    }

    private function __construct() { 
        // Connecting to mysql database
        $this->_conn = new mysqli(DB_HOST, DB_USERNAME, DB_PASSWORD, DB_NAME);

        // Check for database connection error
        if (mysqli_connect_errno()) {
            echo "Failed to connect to MySQL: " . mysqli_connect_error();
            exit;
        }
    }
    // Get connection
    function getConnection() {
        return $this->_conn;
    }

}

To make a connection to database you can do that

$db = DbConnect::getInstance();
$conn = $db->getConnection();
$query= "SELECT foo FROM .....";
$result = $conn->query($query);
mamosek
  • 316
  • 2
  • 6
  • i have updated the question please take a look this is what i was doing but $this for same variable name is creating a problem as soon as i remove it works but i wonder it isn't working this way. And i have do $this->conn because the variable name is same right. So referring the current class variable – SameerKhan1406 Nov 10 '16 at 04:50
  • I dont exacly understand your question now. I can see you have updated your question. U have added a new class. But im interesting why ? What is the purpose? If you want to have connection in one variable just by invoking it with `$db = new DbConnect` so you have to rebuild your `DbConnect class`. However i suppose you have still problem to connect to the server. You have your connection in `conn` variable of `DbHandler class`. But you can't access it outside of this class because it is `private`. Please be more specific so i can help you – mamosek Nov 10 '16 at 07:40
  • i am doing this because i wanted to implement the same thing as this guy did see http://www.androidhive.info/2015/08/android-adding-sms-verification-like-whatsapp-part-1/ this is what i am doing exactly but instead over here i took single file and without requiring or including DbConnect i did simple in single file just to connect to the database i.e in two different classes and secondly both $conn variable are different on from DbConnect class and other from DbHandler class to refer the current i used "this" keyword – SameerKhan1406 Nov 10 '16 at 09:55
  • Ok i can see what you are trying to achieve. Can you reach this php file in your browser? Can you maybe add `error_reporting(E_ALL); ini_set('display_errors', 1);` at the top of file and maybe there are errors showing up? – mamosek Nov 10 '16 at 10:08
  • Please also check if the file `config.php` is in the same directory and also check if it's name is the same as you include (you are using uppercase letter first - `Config.php` – mamosek Nov 10 '16 at 10:15
  • Its in the same directory just besides DbConnect :p Jokes apart the worst part is i can't access even from the browser do i need to write 127.0.0.1 instead of localhost if checking from my browser which i learnt from first comment of this question??? – SameerKhan1406 Nov 10 '16 at 10:20
  • my boss told me told to use slim but since i am android developer i don't know php at all. How i am suppose to make api using slim and also i have started learning android. My android code is working fine. I made another php file where i recieved edit text field which i have posted from my android and saved in my database but when i try to do like this i get stuck with the initial file which is used to connect to my database. – SameerKhan1406 Nov 10 '16 at 10:25
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/127789/discussion-between-sameerkhan1406-and-mamosek). – SameerKhan1406 Nov 10 '16 at 10:27