Reputation: 437
Simple question. I'm still new to OOP and basically learning fundamentals. I have a config.php file which is written bellow.
<?php
$hName = 'localhost'; // Hostname :]
$dbName = 'db'; // Database
$tbAdmin = 'admin'; // Table administrator
$tbPosts = 'posts'; // Table posts
$dbUser = 'phpadmin'; // Database login uname
$dbPass = 'phpadmin'; // Database login pw
?>
This is my functions.php file:
class databaseEstablish {
public $dbc;
/**
* Connect to database (make a connection)
* @return boolean Return true for connected / false for not connected
*/
public function connect() {
require_once 'config.php';
$this->host = $hName;
$this->username = $dbUser;
$this->password = $dbPass;
$this->database = $dbname;
$this->dbc = @mysqli_connect($this->host, $this->username, $this->password, $this->database);
}
Although this should probably have to work, the error message appears with the output (dots replace path, and three other dots replace line which are bellow 'require config.php':
Notice: Undefined variable: hName in ... ...
Notice: Undefined variable: dbUser in ... ...
Notice: Undefined variable: dbPass in ... ...
Notice: Undefined variable: dbname in ... ...
Upvotes: 2
Views: 1276
Reputation: 1560
create config.php file
define("hName", 'localhost'); // Hostname :]
define("dbName", 'db');
define("tbPosts", 'posts');
define("dbUser", 'phpadmin');
define("dbPass", 'phpadmin');
create class.php
require_once 'path_to/config.php';
class databaseEstablish {
public $dbc;
/**
* Connect to database (make a connection)
* @return boolean Return true for connected / false for not connected
*/
public function connect() {
$this->dbc = @mysqli_connect(hName, dbUser, dbPass, dbname);
}
Upvotes: 1
Reputation: 540
And your require once should be like this:
require_once('config.php');
And its better to define your config as constant. Example:
define('hName','localhost');
define('dbUser', 'username');
and use it like this:
@mysqli_connect(hName,dbUser...
and so on. And I suggest don't put @
in front of anything.
It will suppress any error produced when calling that method. Since you are new you shouldn't ignore the error reports. The error reports are very important to troubleshoot your code.
Upvotes: 0
Reputation: 68526
When you add a require_once
it is going to be rendered like this..
public function connect() {
$hName = 'localhost'; // Hostname :]
$dbName = 'db'; // Database
$tbAdmin = 'admin'; // Table administrator
$tbPosts = 'posts'; // Table posts
$dbUser = 'phpadmin'; // Database login uname
$dbPass = 'phpadmin';
$this->host = $hName;
$this->username = $dbUser;
$this->password = $dbPass;
$this->database = $dbname;
which is wrong actually.... Instead add the require_once
outside of the class and pass the arguments to the function..
<?php
require_once 'config.php';
class databaseEstablish {
public $dbc;
/**
* Connect to database (make a connection)
* @return boolean Return true for connected / false for not connected
*/
public function connect($hName,$dbUser,$dbPass,$dbname) {
$this->host = $hName;
$this->username = $dbUser;
$this->password = $dbPass;
$this->database = $dbname;
$this->dbc = mysqli_connect($this->host, $this->username, $this->password, $this->database);
}
}
$dbEst = new databaseEstablish($hName,$dbUser,$dbPass,$dbname);
Upvotes: 0