简体   繁体   中英

I can't connect to my localhost using php

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

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM